<commit_before>/*
 * Copyright (C) 2013 ohmage
 *
 * 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 org.ohmage.fragments;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;

import org.ohmage.app.R;
import org.ohmage.auth.AuthUtil;
import org.ohmage.provider.OhmageContract;
import org.ohmage.provider.OhmageContract.Surveys;

import javax.inject.Inject;

import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate;

/**
 * Created by cketcham on 7/31/13.
 */
public class HomeFragment extends GridFragment implements LoaderCallbacks<Cursor>,
        OnRefreshListener, SyncStatusObserver {

    @Inject AccountManager am;

    private static final String TAG = HomeFragment.class.getSimpleName();
    private PullToRefreshLayout mPullToRefreshLayout;

    private Object syncObserverHandle;
    private Handler mHandler = new Handler();
    private Runnable mRefreshCompleteRunnable = new Runnable() {
        @Override public void run() {
            setEmptyText("No Surveys");
            mPullToRefreshLayout.setRefreshComplete();
        }
    };

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override public void onResume() {
        super.onResume();

        syncObserverHandle = getActivity().getContentResolver().addStatusChangeListener(
                ContentResolver.SYNC_OBSERVER_TYPE_PENDING |
                ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, this);
    }

    @Override public void onPause() {
        super.onPause();
        if(syncObserverHandle != null) {
            getActivity().getContentResolver().removeStatusChangeListener(syncObserverHandle);
            syncObserverHandle = null;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText("No surveys");

        setGridShown(false);

        ListAdapter adapter = new SurveyAdapter(getActivity(), null);
        setListAdapter(adapter);

        getLoaderManager().initLoader(0, null, this);<commit_msg>fixed bug where callback could be run after fragment is destroyed<commit_after>/*
 * Copyright (C) 2013 ohmage
 *
 * 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 org.ohmage.fragments;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;

import org.ohmage.app.R;
import org.ohmage.auth.AuthUtil;
import org.ohmage.provider.OhmageContract;
import org.ohmage.provider.OhmageContract.Surveys;

import javax.inject.Inject;

import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate;

/**
 * Created by cketcham on 7/31/13.
 */
public class HomeFragment extends GridFragment implements LoaderCallbacks<Cursor>,
        OnRefreshListener, SyncStatusObserver {

    @Inject AccountManager am;

    private static final String TAG = HomeFragment.class.getSimpleName();
    private PullToRefreshLayout mPullToRefreshLayout;

    private Object syncObserverHandle;
    private Handler mHandler = new Handler();
    private Runnable mRefreshCompleteRunnable = new Runnable() {
        @Override public void run() {
            if(getView() != null) {
                setEmptyText("No Surveys");
                mPullToRefreshLayout.setRefreshComplete();
            }
        }
    };

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override public void onResume() {
        super.onResume();

        syncObserverHandle = getActivity().getContentResolver().addStatusChangeListener(
                ContentResolver.SYNC_OBSERVER_TYPE_PENDING |
                ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, this);
    }

    @Override public void onPause() {
        super.onPause();
        if(syncObserverHandle != null) {
            getActivity().getContentResolver().removeStatusChangeListener(syncObserverHandle);
            syncObserverHandle = null;
        }
    }

    @Override public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRefreshCompleteRunnable);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText("No surveys");

        setGridShown(false);

        ListAdapter adapter = new SurveyAdapter(getActivity(), null);
        setListAdapter(adapter);

        getLoaderManager().initLoader(0, null, this);
{'fraction_non_alphanumeric': 0.0862991704575863, 'fraction_numerical': 0.004816697886004816, 'mean_word_length': 4.047265361242403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2979368', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1983, 'n_words': 487}