Android Open Source - app_list_widget App Order 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/*from  w  ww  .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.Fragment;
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.*;

public class AppOrderFragment extends Fragment {
    public static final String KEY_ORDERED_APPS =
            "com.acbelter.applistwidget.ui.KEY_ORDERED_APPS";
    private AppListOrderAdapter mListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            ArrayList<AppItem> orderedApps = savedInstanceState
                    .getParcelableArrayList(KEY_ORDERED_APPS);
            mListAdapter = new AppListOrderAdapter(getActivity(), orderedApps);
        } else {
            Bundle args = getArguments();
            ArrayList<AppItem> checkedApps = args.getParcelableArrayList(
                    AppSelectFragment.KEY_CHECKED_APPS);
            ArrayList<String> orderedPackages = args.getStringArrayList(
                    ConfigActivity.KEY_SAVED_ORDERED_PACKAGES);
            sort(checkedApps, orderedPackages);
            mListAdapter = new AppListOrderAdapter(getActivity(), checkedApps);
        }
    }

    /**
     * Sort checkedApps using orderedApps. If item of checkedApps doesn't exist in orderedApps,
     * it is added to the end according to the alphabet.
     */
    private void sort(ArrayList<AppItem> checkedApps,
                      ArrayList<String> orderedApps) {
        TreeMap<Integer, AppItem> appsMap = new TreeMap<Integer, AppItem>();
        ArrayList<AppItem> newApps = new ArrayList<AppItem>();

        outer:
        for (int i = 0; i < checkedApps.size(); i++) {
            for (int j = 0; j < orderedApps.size(); j++) {
                if (checkedApps.get(i).getPackageName().equals(orderedApps.get(j).substring
                        (orderedApps.get(j).indexOf("#") + 1))) {
                    int pos = Integer.parseInt(orderedApps.get(j).substring(0,
                            orderedApps.get(j).indexOf("#")));
                    appsMap.put(pos, checkedApps.get(i));
                    continue outer;
                }
            }
            newApps.add(checkedApps.get(i));
        }

        checkedApps.clear();
        for (Map.Entry<Integer, AppItem> entry : appsMap.entrySet()) {
            checkedApps.add(entry.getValue());
        }
        Collections.sort(newApps);
        checkedApps.addAll(newApps);
    }

    /**
     * @return set of ordinalnumber#com.example.package
     */
    public Set<String> buildOrderedPackages() {
        Set<String> result = new LinkedHashSet<String>(mListAdapter.getCount());
        for (int i = 0; i < mListAdapter.getCount(); i++) {
            result.add(i + "#" + mListAdapter.getItem(i).getPackageName());
        }
        return result;
    }

    public AppListOrderAdapter getAdapter() {
        return mListAdapter;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(KEY_ORDERED_APPS, mListAdapter.getApps());
    }

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




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