Android Open Source - app_list_widget Config Activity






From Project

Back to project page app_list_widget.

License

The source code is released under:

Apache License

If you think the Android project app_list_widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 acbelter//from   w  w w .  j a v a 2 s. c om
 *
 * 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 com.acbelter.applistwidget.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.SearchView.OnQueryTextListener;
import com.acbelter.applistwidget.R;
import com.acbelter.applistwidget.widget.AppListLargeWidgetProvider;
import com.acbelter.applistwidget.widget.AppListWidgetProvider;
import com.acbelter.applistwidget.widget.Pref;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class ConfigActivity extends Activity {
    public static final String KEY_WIDGET_ID =
            "com.acbelter.applistwidget.ui.KEY_WIDGET_ID";
    public static final String KEY_WIDGET_LARGE_FLAG =
            "com.acbelter.applistwidget.ui.KEY_WIDGET_LARGE_FLAG";
    public static final String KEY_SAVED_ORDERED_PACKAGES =
            "com.acbelter.applistwidget.ui.KEY_SAVED_ORDERED_PACKAGES";
    private static final String KEY_CONFIG_WIDGET_FLAG =
            "com.acbelter.applistwidget.ui.KEY_CONFIG_WIDGET_FLAG";
    private int mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private Intent mConfigIntent;
    private ViewGroup mListFrame;
    private Button mNextButton;
    private Button mBackButton;
    private Button mSettingsButton;
    private Menu mMenu;
    private boolean mShowMenuItems;

    private FragmentManager mFragmentManager;
    private SharedPreferences mPref;
    private boolean mWidgetLargeFlag;
    // Indicates that ConfigActivity called from an existing widget
    private boolean mConfigWidgetFlag;
    // Indicates that a new widget was created
    private boolean mWidgetCreatedFlag;

    private OnClickListener mNextListener;
    private OnClickListener mOkListener;
    private OnClickListener mCancelListener;
    private OnClickListener mBackListener;
    private OnClickListener mSettingListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mWidgetId = savedInstanceState.getInt(KEY_WIDGET_ID);
            mConfigWidgetFlag = savedInstanceState.getBoolean(KEY_CONFIG_WIDGET_FLAG);
        } else {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                mConfigWidgetFlag = extras.getBoolean(Pref.FLAG_CONFIG_WIDGET);
            }
        }

        if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        ComponentName provider = appWidgetManager.getAppWidgetInfo(mWidgetId).provider;
        if (provider.getClassName().equals(AppListLargeWidgetProvider.class.getName())) {
            mWidgetLargeFlag = true;
        }

        if (Pref.isPrefExists(mWidgetId)) {
            mPref = getSharedPreferences(Pref.APP_LIST_WIDGET_PREF_ + mWidgetId, MODE_PRIVATE);
        } else {
            createDefaultPreferences();
        }

        setContentView(R.layout.activity_config);
        mFragmentManager = getFragmentManager();

        mListFrame = (ViewGroup) findViewById(R.id.list_frame);
        mNextButton = (Button) findViewById(R.id.next_button);
        mBackButton = (Button) findViewById(R.id.back_button);
        mSettingsButton = (Button) findViewById(R.id.settings_button);
        initButtonsListeners();
        mSettingsButton.setOnClickListener(mSettingListener);

        // No fragments
        if (mFragmentManager.findFragmentByTag(AppSelectFragment.class.getName()) == null &&
                mFragmentManager.findFragmentByTag(AppOrderFragment.class.getName()) == null) {
            Bundle args = new Bundle();
            args.putParcelableArrayList(AppSelectFragment.KEY_CHECKED_APPS,
                    getCheckedAppsFromPref());

            AppSelectFragment asf = new AppSelectFragment();
            asf.setArguments(args);
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.replace(mListFrame.getId(), asf, AppSelectFragment.class.getName());
            transaction.commit();
            mNextButton.setOnClickListener(mNextListener);
            mBackButton.setOnClickListener(mCancelListener);
            mShowMenuItems = true;
        }
        // AppSelectFragment
        else if (mFragmentManager.findFragmentByTag(AppSelectFragment.class.getName()) != null &&
                mFragmentManager.findFragmentByTag(AppOrderFragment.class.getName()) == null) {
            mNextButton.setOnClickListener(mNextListener);
            mBackButton.setOnClickListener(mCancelListener);
            mShowMenuItems = true;
        }
        // AppOrderFragment
        else if (mFragmentManager.findFragmentByTag(AppSelectFragment.class.getName()) == null &&
                mFragmentManager.findFragmentByTag(AppOrderFragment.class.getName()) != null) {
            mNextButton.setOnClickListener(mOkListener);
            mNextButton.setText(getResources().getString(R.string.ok));
            mBackButton.setOnClickListener(mBackListener);
            mBackButton.setText(getResources().getString(R.string.back));
            mShowMenuItems = false;
        } else if (mFragmentManager.findFragmentByTag(AppSelectFragment.class.getName()) != null &&
                mFragmentManager.findFragmentByTag(AppOrderFragment.class.getName()) != null) {
            throw new IllegalStateException("AppSelectFragment and AppOrderFragment is in the " +
                    "FragmentManager in the same time");
        }

        mConfigIntent = new Intent();
        mConfigIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
        setResult(RESULT_CANCELED, mConfigIntent);
    }

    private ArrayList<AppItem> getCheckedAppsFromPref() {
        Set<String> orderedPackages = mPref.getStringSet(Pref.WIDGET_APPS_ + mWidgetId,
                new HashSet<String>());
        ArrayList<AppItem> result = new ArrayList<AppItem>(orderedPackages.size());
        PackageManager packageManager = getPackageManager();
        for (String item : orderedPackages) {
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(
                        item.substring(item.indexOf("#") + 1), 0);
                result.add(new AppItem(info, packageManager));
            } catch (NameNotFoundException e) {
                // Ignore and continue
            }
        }
        return result;
    }

    private void initButtonsListeners() {
        mNextListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppSelectFragment asf = (AppSelectFragment) mFragmentManager.findFragmentByTag
                        (AppSelectFragment.class.getName());
                ArrayList<AppItem> checkedApps = asf.getAdapter().getCheckedApps();
                if (checkedApps.isEmpty()) {
                    Toast.makeText(ConfigActivity.this, R.string.select_app_toast,
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                Bundle args = new Bundle();
                args.putParcelableArrayList(AppSelectFragment.KEY_CHECKED_APPS, checkedApps);
                ArrayList<String> orderedPackages =  new ArrayList<String>(
                    mPref.getStringSet(Pref.WIDGET_APPS_ + mWidgetId, new HashSet<String>()));
                args.putStringArrayList(KEY_SAVED_ORDERED_PACKAGES, orderedPackages);

                AppOrderFragment aof = new AppOrderFragment();
                aof.setArguments(args);
                FragmentTransaction transaction = mFragmentManager.beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.replace(mListFrame.getId(), aof, AppOrderFragment.class.getName());
                transaction.commit();

                mShowMenuItems = false;
                MenuItem searchItem = mMenu.findItem(R.id.search_item).setVisible(false);
                searchItem.collapseActionView();
                mMenu.findItem(R.id.refresh_item).setVisible(false);

                mNextButton.setOnClickListener(mOkListener);
                mNextButton.setText(getResources().getString(R.string.ok));

                mBackButton.setOnClickListener(mBackListener);
                mBackButton.setText(getResources().getString(R.string.back));
            }
        };

        mOkListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppOrderFragment aof = (AppOrderFragment) mFragmentManager
                        .findFragmentByTag(AppOrderFragment.class.getName());
                Set<String> apps = aof.buildOrderedPackages();
                Editor editor = mPref.edit();
                editor.putStringSet(Pref.WIDGET_APPS_ + mWidgetId, apps);
                editor.commit();
                AppWidgetManager appWidgetManager =
                        AppWidgetManager.getInstance(ConfigActivity.this);
                AppListWidgetProvider.updateWidget(ConfigActivity.this, appWidgetManager,
                        mWidgetId);
                setResult(RESULT_OK, mConfigIntent);
                mWidgetCreatedFlag = true;
                finish();
            }
        };

        mBackListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppOrderFragment aof = (AppOrderFragment) mFragmentManager
                        .findFragmentByTag(AppOrderFragment.class.getName());
                Bundle args = new Bundle();
                args.putParcelableArrayList(AppSelectFragment.KEY_CHECKED_APPS,
                        aof.getAdapter().getApps());

                AppSelectFragment asf = new AppSelectFragment();
                asf.setArguments(args);
                FragmentTransaction transaction = mFragmentManager.beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                transaction.replace(mListFrame.getId(), asf, AppSelectFragment.class.getName());
                transaction.commit();

                mShowMenuItems = true;
                mMenu.findItem(R.id.search_item).setVisible(true);
                mMenu.findItem(R.id.refresh_item).setVisible(true);
                initSearchView(asf.getAdapter());

                mNextButton.setOnClickListener(mNextListener);
                mNextButton.setText(getResources().getString(R.string.next));

                mBackButton.setOnClickListener(mCancelListener);
                mBackButton.setText(getResources().getString(R.string.cancel));
            }
        };

        mCancelListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };

        mSettingListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent settingsIntent = new Intent(ConfigActivity.this, SettingsActivity.class);
                settingsIntent.putExtra(KEY_WIDGET_ID, mWidgetId);
                settingsIntent.putExtra(KEY_WIDGET_LARGE_FLAG, mWidgetLargeFlag);
                startActivity(settingsIntent);
            }
        };
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_WIDGET_ID, mWidgetId);
        outState.putBoolean(KEY_CONFIG_WIDGET_FLAG, mConfigWidgetFlag);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!mConfigWidgetFlag && !mWidgetCreatedFlag) {
            Pref.deletePref(mWidgetId);
        }
    }

    private void createDefaultPreferences() {
        mPref = getSharedPreferences(Pref.APP_LIST_WIDGET_PREF_ + mWidgetId, MODE_PRIVATE);
        Editor editor = mPref.edit();
        if (mWidgetLargeFlag) {
            editor.putInt(Pref.WIDGET_ICONS_SIZE_ + mWidgetId,
                    getResources().getInteger(R.integer.default_large_icons_size));
        } else {
            editor.putInt(Pref.WIDGET_ICONS_SIZE_ + mWidgetId,
                    getResources().getInteger(R.integer.default_icons_size));
        }
        editor.putString(Pref.WIDGET_ICONS_ALIGN_ + mWidgetId,
                getResources().getString(R.string.default_icons_align));
        editor.putBoolean(Pref.WIDGET_SHOW_SETTINGS_ICON_ + mWidgetId,
                getResources().getBoolean(R.bool.default_show_settings_icon));
        editor.putBoolean(Pref.WIDGET_SHOW_BACKGROUND_ + mWidgetId,
                getResources().getBoolean(R.bool.default_show_background));
        editor.putString(Pref.WIDGET_COLOR_ + mWidgetId,
                getResources().getString(R.string.default_color));
        editor.putInt(Pref.WIDGET_TRANSPARENCY_ + mWidgetId,
                getResources().getInteger(R.integer.default_transparency));
        editor.putStringSet(Pref.WIDGET_APPS_ + mWidgetId, new HashSet<String>());
        editor.commit();
    }

    void initSearchView(final Filterable target) {
        MenuItem searchItem = mMenu.findItem(R.id.search_item);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setQueryHint(getResources().getString(R.string.search));
        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                target.getFilter().filter(newText);
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        mMenu = menu;
        menu.findItem(R.id.refresh_item).setVisible(mShowMenuItems);

        MenuItem searchItem = menu.findItem(R.id.search_item);
        searchItem.setVisible(mShowMenuItems);

        if (!mShowMenuItems) {
            searchItem.collapseActionView();
        } else {
            AppSelectFragment asf = (AppSelectFragment) mFragmentManager
                    .findFragmentByTag(AppSelectFragment.class.getName());
            if (asf != null) {
                initSearchView(asf.getAdapter());
            }
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh_item: {
                AppSelectFragment asf = (AppSelectFragment) mFragmentManager.findFragmentByTag
                        (AppSelectFragment.class.getName());
                asf.reload();
                return true;
            }
            case R.id.about_item: {
                LayoutInflater inflater = getLayoutInflater();
                View aboutView = inflater.inflate(R.layout.about, null);
                TextView email = (TextView) aboutView.findViewById(R.id.author_email);
                Linkify.addLinks(email, Pattern.compile("^.+$"), "mailto:");

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.about);
                builder.setView(aboutView);
                builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
                builder.show();
                return true;
            }
            default: {
                return super.onOptionsItemSelected(item);
            }
        }
    }
}




Java Source Code List

com.acbelter.applistwidget.ui.AppItem.java
com.acbelter.applistwidget.ui.AppListOrderAdapter.java
com.acbelter.applistwidget.ui.AppListSelectAdapter.java
com.acbelter.applistwidget.ui.AppOrderFragment.java
com.acbelter.applistwidget.ui.AppSelectFragment.java
com.acbelter.applistwidget.ui.ConfigActivity.java
com.acbelter.applistwidget.ui.SeekBarPreference.java
com.acbelter.applistwidget.ui.SettingsActivity.java
com.acbelter.applistwidget.ui.SettingsFragment.java
com.acbelter.applistwidget.widget.AppListLargeWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetFactory.java
com.acbelter.applistwidget.widget.AppListWidgetProvider.java
com.acbelter.applistwidget.widget.AppListWidgetService.java
com.acbelter.applistwidget.widget.Pref.java