Android Open Source - app_list_widget App Select Fragment






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//w  ww .j a  v  a2s .  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.Fragment;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.acbelter.applistwidget.R;

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

public class AppSelectFragment extends Fragment {
    public static final String KEY_CHECKED_APPS =
            "com.acbelter.applistwidget.ui.KEY_CHECKED_APPS";
    private static final String KEY_APPS_LIST =
            "com.acbelter.applistwidget.ui.KEY_APPS_LIST";
    private ListView mListView;
    private AppListSelectAdapter mListAdapter;
    private ArrayList<AppItem> mAppItems;
    private PackageManager mPackageManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mAppItems = savedInstanceState.getParcelableArrayList(KEY_APPS_LIST);
            ArrayList<AppItem> checkedApps = savedInstanceState
                    .getParcelableArrayList(KEY_CHECKED_APPS);
            mListAdapter = new AppListSelectAdapter(getActivity(), mAppItems, checkedApps);
        } else {
            Bundle args = getArguments();
            ArrayList<AppItem> checkedApps =
                    args.getParcelableArrayList(KEY_CHECKED_APPS);
            new LoadAppTask().execute(checkedApps);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mPackageManager = activity.getPackageManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select, container, false);
        mListView = (ListView) view.findViewById(R.id.app_list);
        mListView.setAdapter(mListAdapter);
        return view;
    }

    public AppListSelectAdapter getAdapter() {
        return mListAdapter;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(KEY_APPS_LIST, mAppItems);
        outState.putParcelableArrayList(KEY_CHECKED_APPS, mListAdapter.getCheckedApps());
    }

    public void reload() {
        new LoadAppTask().execute(mListAdapter.getCheckedApps());
    }

    private ArrayList<ApplicationInfo> getAppsWithPackage(List<ApplicationInfo> infoList) {
        ArrayList<ApplicationInfo> result = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : infoList) {
            if (mPackageManager.getLaunchIntentForPackage(info.packageName) != null) {
                result.add(info);
            }
        }
        return result;
    }

    class LoadAppTask extends AsyncTask<ArrayList<AppItem>, Void, Void> {
        private ProgressDialog mProgressDialog;

        @Override
        protected Void doInBackground(ArrayList<AppItem>... params) {
            List<ApplicationInfo> appsList = getAppsWithPackage(mPackageManager
                    .getInstalledApplications(PackageManager.GET_META_DATA));
            mAppItems = new ArrayList<AppItem>(appsList.size());
            for (int i = 0; i < appsList.size(); i++) {
                mAppItems.add(new AppItem(appsList.get(i), mPackageManager));
            }
            Collections.sort(mAppItems);
            mListAdapter = new AppListSelectAdapter(getActivity(), mAppItems, params[0]);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mListView.setAdapter(mListAdapter);
            ((ConfigActivity) getActivity()).initSearchView(mListAdapter);
            mProgressDialog.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(getActivity(), null,
                    getResources().getString(R.string.loading));
            super.onPreExecute();
        }
    }
}




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