Android Open Source - app_list_widget App List Widget Factory






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 w  w.  j  a va  2s .  c  o m*/
 *
 * 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.widget;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;
import com.acbelter.applistwidget.R;

import java.util.*;

public class AppListWidgetFactory implements RemoteViewsFactory {
    private static final int SIZE_VERY_SMALL = 24;
    private static final int SIZE_SMALL = 32;
    private static final int SIZE_MEDIUM = 48;
    private static final int SIZE_LARGE = 72;
    private static final int SIZE_VERY_LARGE = 96;
    private static final int ALIGN_LEFT = 1;
    private static final int ALIGN_CENTER = 2;
    private static final int ALIGN_RIGHT = 3;
    private Context mContext;
    private int mWidgetId;
    private List<ApplicationInfo> mWidgetApps;
    private PackageManager mPackageManager;
    private SharedPreferences mPref;
    private int mIconsSize;
    // For using in switch operator
    private int mIconsAlignConst;

    public AppListWidgetFactory(Context context, Intent intent) {
        mContext = context;
        mWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        mPackageManager = mContext.getPackageManager();
        mPref = mContext.getSharedPreferences(Pref.APP_LIST_WIDGET_PREF_ + mWidgetId,
                Context.MODE_PRIVATE);
    }

    private static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    private List<ApplicationInfo> getWidgetApps() {
        Set<String> orderedApps = mPref.getStringSet(Pref.WIDGET_APPS_ + mWidgetId,
                new HashSet<String>(0));
        TreeMap<Integer, ApplicationInfo> appsMap = new TreeMap<Integer, ApplicationInfo>();

        ApplicationInfo info;
        for (String str : orderedApps) {
            int pos = Integer.parseInt(str.substring(0, str.indexOf("#")));
            try {
                info = mPackageManager.getApplicationInfo(str.substring(
                        str.indexOf("#") + 1), 0);
            } catch (NameNotFoundException e) {
                info = null;
            }
            if (info != null) {
                appsMap.put(pos, info);
            }
        }

        List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>(appsMap.size());
        for (Map.Entry<Integer, ApplicationInfo> entry : appsMap.entrySet()) {
            apps.add(entry.getValue());
        }

        if (mPref.getBoolean(Pref.WIDGET_SHOW_SETTINGS_ICON_ + mWidgetId, false)) {
            try {
                apps.add(mPackageManager.getApplicationInfo(Pref.PACKAGE_THIS, 0));
            } catch (NameNotFoundException e) {
                // Ignore because THIS package always exists =)
            }
        }

        return apps;
    }

    @Override
    public void onCreate() {
        mWidgetApps = getWidgetApps();
    }

    @Override
    public void onDataSetChanged() {
        mWidgetApps.clear();
        mWidgetApps.addAll(getWidgetApps());
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public int getCount() {
        return mWidgetApps.size();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        mIconsSize = mPref.getInt(Pref.WIDGET_ICONS_SIZE_ + mWidgetId, SIZE_MEDIUM);
        String align = mPref.getString(Pref.WIDGET_ICONS_ALIGN_ + mWidgetId, "center");
        if (align.equals("left")) {
            mIconsAlignConst = ALIGN_LEFT;
        } else if (align.equals("center")) {
            mIconsAlignConst = ALIGN_CENTER;
        } else if (align.equals("right")) {
            mIconsAlignConst = ALIGN_RIGHT;
        }

        RemoteViews remoteView;
        String name = mContext.getPackageName();
        // This sum will always be different for each pair
        switch (mIconsAlignConst + mIconsSize) {
            case SIZE_VERY_SMALL + ALIGN_LEFT: {
                remoteView = new RemoteViews(name, R.layout.item_very_small_left);
                break;
            }
            case SIZE_VERY_SMALL + ALIGN_CENTER: {
                remoteView = new RemoteViews(name, R.layout.item_very_small_center);
                break;
            }
            case SIZE_VERY_SMALL + ALIGN_RIGHT: {
                remoteView = new RemoteViews(name, R.layout.item_very_small_right);
                break;
            }

            case SIZE_SMALL + ALIGN_LEFT: {
                remoteView = new RemoteViews(name, R.layout.item_small_left);
                break;
            }
            case SIZE_SMALL + ALIGN_CENTER: {
                remoteView = new RemoteViews(name, R.layout.item_small_center);
                break;
            }
            case SIZE_SMALL + ALIGN_RIGHT: {
                remoteView = new RemoteViews(name, R.layout.item_small_right);
                break;
            }

            case SIZE_MEDIUM + ALIGN_LEFT: {
                remoteView = new RemoteViews(name, R.layout.item_medium_left);
                break;
            }
            case SIZE_MEDIUM + ALIGN_CENTER: {
                remoteView = new RemoteViews(name, R.layout.item_medium_center);
                break;
            }
            case SIZE_MEDIUM + ALIGN_RIGHT: {
                remoteView = new RemoteViews(name, R.layout.item_medium_right);
                break;
            }

            case SIZE_LARGE + ALIGN_LEFT: {
                remoteView = new RemoteViews(name, R.layout.item_large_left);
                break;
            }
            case SIZE_LARGE + ALIGN_CENTER: {
                remoteView = new RemoteViews(name, R.layout.item_large_center);
                break;
            }
            case SIZE_LARGE + ALIGN_RIGHT: {
                remoteView = new RemoteViews(name, R.layout.item_large_right);
                break;
            }

            case SIZE_VERY_LARGE + ALIGN_LEFT: {
                remoteView = new RemoteViews(name, R.layout.item_very_large_left);
                break;
            }
            case SIZE_VERY_LARGE + ALIGN_CENTER: {
                remoteView = new RemoteViews(name, R.layout.item_very_large_center);
                break;
            }
            case SIZE_VERY_LARGE + ALIGN_RIGHT: {
                remoteView = new RemoteViews(name, R.layout.item_very_large_right);
                break;
            }

            default: {
                throw new IllegalArgumentException("Incorrect list item configuration");
            }
        }

        Drawable icon;
        if (!mWidgetApps.get(position).packageName.equals(Pref.PACKAGE_THIS)) {
            icon = mWidgetApps.get(position).loadIcon(mPackageManager);
        } else {
            icon = mContext.getResources().getDrawable(R.drawable.ic_settings);
        }
        remoteView.setImageViewBitmap(R.id.widget_app_icon, drawableToBitmap(icon));

        Intent clickIntent = new Intent();
        clickIntent.putExtra(AppListWidgetProvider.ITEM_PACKAGE,
                mWidgetApps.get(position).packageName);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
        remoteView.setOnClickFillInIntent(R.id.widget_app_icon, clickIntent);

        return remoteView;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 15;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}




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