/*
 * Copyright 2014 Realm Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.realm.examples.threads;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import io.realm.Realm;
import io.realm.examples.threads.model.Score;

/**
 * This fragment demonstrates how Realm can work with AsyncTasks.
 */
public class AsyncTaskFragment extends Fragment {

    private static final String TAG = AsyncTaskFragment.class.getName();
    private static final int TEST_OBJECTS = 100;

    private LinearLayout logsView;
    private ProgressBar progressView;
    private ImportAsyncTask asyncTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_asynctask, container, false);
        logsView = (LinearLayout) rootView.findViewById(R.id.logs);
        progressView = (ProgressBar) rootView.findViewById(R.id.progressBar);
        progressView.setVisibility(View.GONE);
        rootView.findViewById(R.id.start_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (asyncTask != null) {
                    asyncTask.cancel(true);
                }

                asyncTask = new ImportAsyncTask();
                asyncTask.execute();
            }
        });

        return rootView;
    }

    private void showStatus(String txt) {
        Log.i(TAG, txt);
        TextView tv = new TextView(getActivity());
        tv.setText(txt);
        tv.setTextColor(getResources().getColor(android.R.color.white));
        logsView.addView(tv);
    }

    // ASyncTask that imports Realm data while providing progress and returns the value of an
    // aggregate function in the end.
    //
    // Note:
    // doInBackground() runs in its own background thread while all other methods are executed on the
    // UI thread. This means that it is not possible to reuse RealmObjects or RealmResults created
    // in doInBackground() in the other methods. Nor is it possible to use RealmObjects as Progress
    // or Result objects.
    private class ImportAsyncTask extends AsyncTask<Void, Integer, Integer> {

        @Override
        protected Integer doInBackground(Void... params) {
            Realm realm = Realm.getDefaultInstance();

            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    realm.delete(Score.class);
                    for (int i = 0; i < TEST_OBJECTS; i++) {
                        if (isCancelled()) break;
                        Score score = realm.createObject(Score.class);
                        score.setName("Name" + i);
                        score.setScore(i);
                    }
                }
            });

            Number sum = realm.where(Score.class).sum("score");
            realm.close();
            return sum.intValue();
        }

        @Override
        protected void onPreExecute() {
            logsView.removeAllViews();
            progressView.setVisibility(View.VISIBLE);
            showStatus("Starting import");
        }

        @Override
        protected void onPostExecute(Integer sum) {
            progressView.setVisibility(View.GONE);
            showStatus(TEST_OBJECTS + " objects imported.");
            showStatus("The total score is : " + sum);
        }
    }
}

@@ -968,16 +968,53 @@
 tView;%0A%0A
+import java.lang.ref.WeakReference;%0A%0A
 import i
@@ -2149,16 +2149,38 @@
 yncTask(
+AsyncTaskFragment.this
 );%0A     
@@ -2266,24 +2266,176 @@
 iew;%0A    %7D%0A%0A
+    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A        if (asyncTask != null) %7B%0A            asyncTask.cancel(false);%0A        %7D%0A    %7D%0A%0A
     private 
@@ -3166,16 +3166,23 @@
 private 
+static 
 class Im
@@ -3236,24 +3236,242 @@
 Integer%3E %7B%0A%0A
+        private final WeakReference%3CAsyncTaskFragment%3E fragmentRef;%0A%0A        ImportAsyncTask(AsyncTaskFragment outerFragment) %7B%0A            fragmentRef = new WeakReference%3CAsyncTaskFragment%3E(outerFragment);%0A        %7D%0A%0A
         @Ove
@@ -4309,32 +4309,237 @@
 ) %7B%0A            
+final AsyncTaskFragment fragment = fragmentRef.get();%0A            if (fragment == null %7C%7C fragment.isDetached()) %7B%0A                cancel(false);%0A                return;%0A            %7D%0A            fragment.
 logsView.removeA
@@ -4553,32 +4553,41 @@
 ();%0A            
+fragment.
 progressView.set
@@ -4616,32 +4616,41 @@
 E);%0A            
+fragment.
 showStatus(%22Star
@@ -4749,32 +4749,206 @@
 ) %7B%0A            
+final AsyncTaskFragment fragment = fragmentRef.get();%0A            if (fragment == null %7C%7C fragment.isDetached()) %7B%0A                return;%0A            %7D%0A            fragment.
 progressView.set
@@ -4974,32 +4974,41 @@
 E);%0A            
+fragment.
 showStatus(TEST_
@@ -5052,16 +5052,25 @@
         
+fragment.
 showStat

Fix Context leak warning in threadExample (#4580) (#4586)