Android Open Source - app_list_widget App List Widget Provider






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 ww w.  java  2 s  .co  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.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.acbelter.applistwidget.R;
import com.acbelter.applistwidget.ui.ConfigActivity;

public class AppListWidgetProvider extends AppWidgetProvider {
    protected static final String ACTION_ITEM_CLICK =
            "com.acbelter.applistwidget.widget.ACTION_ITEM_CLICK";
    public final static String ITEM_PACKAGE =
            "com.acbelter.applistwidget.widget.ITEM_PACKAGE";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int id : appWidgetIds) {
            updateWidget(context, appWidgetManager, id);
        }
    }

    // There is the bug https://code.google.com/p/android/issues/detail?id=3696
    public static void updateWidget(Context context, AppWidgetManager appWidgetManager,
                                    int widgetId) {
        SharedPreferences sp = context.getSharedPreferences(Pref.APP_LIST_WIDGET_PREF_ +
                widgetId, Context.MODE_PRIVATE);
        RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget);
        boolean showBackground = sp.getBoolean(Pref.WIDGET_SHOW_BACKGROUND_ + widgetId, false);
        if (showBackground) {
            String strColor = sp.getString(Pref.WIDGET_COLOR_ + widgetId,
                context.getResources().getString(R.string.default_color));
            int alpha = sp.getInt(Pref.WIDGET_TRANSPARENCY_ + widgetId,
                context.getResources().getInteger(R.integer.default_transparency));
            int color = buildColor(strColor, alpha);
            widgetView.setInt(R.id.widget_layout, "setBackgroundColor", color);
        } else {
            widgetView.setInt(R.id.widget_layout, "setBackgroundColor", Color.TRANSPARENT);
        }
        // Set list adapter
        Intent adapter = new Intent(context, AppListWidgetService.class);
        adapter.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
        // For tell one intent from the other (for using different widget adapters)
        Uri data = Uri.parse(adapter.toUri(Intent.URI_INTENT_SCHEME));
        adapter.setData(data);
        widgetView.setRemoteAdapter(R.id.widget_app_list, adapter);

        setListClick(context, widgetView);

        appWidgetManager.updateAppWidget(widgetId, widgetView);
        appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.widget_app_list);
    }

    protected static int buildColor(String strColor, int alpha) {
        int color = Color.parseColor(strColor);
        color = Color.argb((int) ((100 - alpha) * 2.55f),
                Color.red(color), Color.green(color), Color.blue(color));
        return color;
    }

    protected static void setListClick(Context context, RemoteViews remoteViews) {
        Intent clickIntent = new Intent(context, AppListWidgetProvider.class);
        clickIntent.setAction(ACTION_ITEM_CLICK);
        PendingIntent clickPendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
        remoteViews.setPendingIntentTemplate(R.id.widget_app_list, clickPendingIntent);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        for (int widgetId : appWidgetIds) {
            Pref.deletePref(widgetId);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equalsIgnoreCase(ACTION_ITEM_CLICK)) {
            String packageName = intent.getStringExtra(ITEM_PACKAGE);
            PackageManager manager = context.getPackageManager();
            Intent appIntent = manager.getLaunchIntentForPackage(packageName);
            if (appIntent != null) {
                appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                context.startActivity(appIntent);
            } else if (packageName.equals(Pref.PACKAGE_THIS)) {
                Intent configIntent = new Intent(context, ConfigActivity.class);
                int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
                configIntent.putExtra(Pref.FLAG_CONFIG_WIDGET, true);
                configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(configIntent);
            } else {
                Toast.makeText(context, R.string.invalid_app_toast, Toast.LENGTH_SHORT).show();
            }
        }
    }
}




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