org.akop.crosswords.fragment.BaseFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.fragment.BaseFragment.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package org.akop.crosswords.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.SparseBooleanArray;
import android.util.TypedValue;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseFragment extends Fragment {
    private final Object mTaskLock = new Object();

    private List<AsyncTask> mActiveTasks;
    protected Handler mHandler = new Handler();

    public interface MessageBarInterface {
        public void hideMessageBar();

        public void showMessage(String message, int actionTextResId, final Runnable onActionClicked);
    }

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

        mActiveTasks = new ArrayList<>();
    }

    protected void addTask(AsyncTask task) {
        synchronized (mTaskLock) {
            mActiveTasks.add(task);
        }
    }

    protected void removeTask(AsyncTask task) {
        synchronized (mTaskLock) {
            mActiveTasks.remove(task);
        }
    }

    protected void cancelAllTasks() {
        synchronized (mTaskLock) {
            for (AsyncTask task : mActiveTasks) {
                try {
                    task.cancel(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            mActiveTasks.clear();
        }
    }

    protected boolean anyActiveTasks() {
        return mActiveTasks.size() > 0;
    }

    protected void hideMessageBar() {
        Activity activity = getActivity();
        if (activity instanceof MessageBarInterface) {
            ((MessageBarInterface) activity).hideMessageBar();
        }
    }

    protected void showMessage(String message) {
        Activity activity = getActivity();
        if (activity instanceof MessageBarInterface) {
            ((MessageBarInterface) activity).showMessage(message, 0, null);
        }
    }

    protected void showMessage(int messageResId, int actionTextResId, Runnable onActionClicked) {
        showMessage(getString(messageResId), actionTextResId, onActionClicked);
    }

    protected void showMessage(String message, int actionTextResId, Runnable onActionClicked) {
        Activity activity = getActivity();
        if (activity instanceof MessageBarInterface) {
            ((MessageBarInterface) activity).showMessage(message, actionTextResId, onActionClicked);
        }
    }

    protected void readSparseBooleanArray(Bundle bundle, String prefix, SparseBooleanArray array) {
        int keys[] = bundle.getIntArray(prefix + "_keys");
        boolean values[] = bundle.getBooleanArray(prefix + "_values");

        if (keys != null && values != null) {
            for (int i = 0, n = keys.length; i < n; i++) {
                array.put(keys[i], values[i]);
            }
        }
    }

    protected void putSparseBooleanArray(Bundle bundle, String prefix, SparseBooleanArray array) {
        int size = array.size();
        int keys[] = new int[size];
        boolean values[] = new boolean[size];

        for (int i = 0; i < size; i++) {
            keys[i] = array.keyAt(i);
            values[i] = array.valueAt(i);
        }

        bundle.putIntArray(prefix + "_keys", keys);
        bundle.putBooleanArray(prefix + "_values", values);
    }

    public static int getThemedColor(Context context, int attrId) {
        Resources.Theme theme = context.getTheme();
        TypedValue typedValue = new TypedValue();
        theme.resolveAttribute(attrId, typedValue, true);
        return typedValue.data;
    }

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

        hideMessageBar();
    }

    @Override
    public void onDestroy() {
        // Cancel any ongoing AsyncTask's
        cancelAllTasks();

        super.onDestroy();
    }
}