Example usage for android.appwidget AppWidgetManager getInstance

List of usage examples for android.appwidget AppWidgetManager getInstance

Introduction

In this page you can find the example usage for android.appwidget AppWidgetManager getInstance.

Prototype

public static AppWidgetManager getInstance(Context context) 

Source Link

Document

Get the AppWidgetManager instance to use for the supplied android.content.Context Context object.

Usage

From source file:com.android.gallery3d.gadget.WidgetConfigure.java

private void updateWidgetAndFinish(WidgetDatabaseHelper.Entry entry) {
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    RemoteViews views = PhotoAppWidgetProvider.buildWidget(this, mAppWidgetId, entry);
    manager.updateAppWidget(mAppWidgetId, views);
    setResult(RESULT_OK, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
    finish();/*from ww w . j  ava  2  s.  c o  m*/
}

From source file:com.launcher.silverfish.launcher.homescreen.HomeScreenFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    sqlHelper = new LauncherSQLiteHelper((App) getActivity().getApplication());
    settings = new Settings(getContext());

    // Initiate global variables
    mAppWidgetManager = AppWidgetManager.getInstance(getActivity().getBaseContext());
    mAppWidgetHost = new LauncherAppWidgetHost(getActivity().getApplicationContext(), WIDGET_HOST_ID);
    mAppWidgetHost.startListening();//w  w  w .  j a v  a  2  s.c  o  m

    mPacMan = getActivity().getPackageManager();
    appsList = new ArrayList<>();

    rootView = inflater.inflate(R.layout.activity_home, container, false);
    // Set touch slop and listen for touch events, such as swipe
    touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    rootView.setOnTouchListener(onRootTouchListener);

    shortcutLayout = (SquareGridLayout) rootView.findViewById(R.id.shortcut_area);

    // Start listening for shortcut additions
    ((LauncherActivity) getActivity()).setFragShortcutAddListenerRefreshListener(new ShortcutAddListener() {
        @Override
        public void OnShortcutAdd(String appName) {
            // Insert it into the database and get the row id
            // TODO: Check if an error has occurred while inserting into database.
            if (sqlHelper.canAddShortcut(appName)) {
                long appId = sqlHelper.addShortcut(appName);

                // Create shortcut and add it
                ShortcutTable shortcut = new ShortcutTable();
                shortcut.setPackageName(appName);
                shortcut.setId(appId);
                if (addAppToView(shortcut)) {
                    updateShortcuts();
                }
            }
        }
    });

    addWidgetOnClickListener();
    setOnDragListener();

    setWidgetColors(settings.getWidgetBgColor(), settings.getFontFgColor());
    setWidgetVisibility(settings.isWidgetVisible());
    loadWidget();

    loadApps();
    updateShortcuts();

    return rootView;
}

From source file:com.concentriclivers.mms.com.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId/*from   w w  w  .  j av  a2  s  . c om*/
 */
private static void updateWidget(Context context, int appWidgetId) {
    //        if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
    Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
    //        }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    PendingIntent clickIntent;

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, MmsWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);

    remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.app_label));

    // Open Mms's app conversation list when click on header
    final Intent convIntent = new Intent(context, ConversationList.class);
    clickIntent = PendingIntent.getActivity(context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
    composeIntent.setAction(Intent.ACTION_SENDTO);
    clickIntent = PendingIntent.getActivity(context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ComposeMessageActivity.class);
    Intent msgIntent = new Intent(Intent.ACTION_VIEW);
    msgIntent.setType("vnd.android-dir/mms-sms");
    taskStackBuilder.addNextIntent(msgIntent);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list,
            taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
}

From source file:co.carlosjimenez.android.currencyalerts.app.widget.DetailWidgetProvider.java

@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
    super.onReceive(context, intent);
    if (ForexSyncAdapter.ACTION_DATA_UPDATED.equals(intent.getAction())) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_list);
    }//from   w ww.j a v a  2  s  .  c  o  m
}

From source file:ru.kaefik.isaifutdinov.an_weather_widget.ConfigActivity.java

@Override
protected void onStart() {
    super.onStart();
    Log.i(TAG_SERVICE, "onStart  ConfigActivity");
    //-----------------------
    mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    Intent intent = getIntent();// w  w w.  j a v a 2  s .  co  m
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    final Context context = this;

    //-----------------------

    //  ??    ? ??
    mNameCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String cityNameString = adapter.getCityModel(position);

            Log.i(TAG_SERVICE, " OnItemClick  ConfigActivity ->   " + cityNameString
                    + "  id : " + String.valueOf(mAppWidgetId));

            saveStringParametersToCfg(context, String.valueOf(mAppWidgetId), cityNameString);

            Intent resulValue = new Intent(AnWeatherWidget.CLICK_WIDGET_BUTTON);
            resulValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            //  ?  ConfigActivity
            try {
                AnWeatherWidget.updateAppWidget(context, AppWidgetManager.getInstance(context), mAppWidgetId);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            setResult(RESULT_OK, resulValue);
            finish();
        }
    });
}

From source file:com.dpcsoftware.mn.App.java

public void setFlag(int tableId) {
    switch (tableId) {
    case 1:// w  w  w .j a v  a2  s  . c  om
        mnUpdateList = true;

        //Update widgets if anyone is being used
        AppWidgetManager wManager = AppWidgetManager.getInstance(this);
        ComponentName cWidgetProvider = new ComponentName(this, Widget1.class);
        int wIds[] = wManager.getAppWidgetIds(cWidgetProvider);
        if (wIds.length != 0) {
            Intent updateIntent = new Intent(this, Widget1.class);
            updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, wIds);
            try {
                PendingIntent.getBroadcast(this, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT).send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
        break;
    case 2:
        mnUpdateList = true;
        break;
    case 3:
        mnUpdateMenu = true;
        break;
    }
}

From source file:com.dycody.android.idealnote.BaseActivity.java

/**
 * Notifies App Widgets about data changes so they can update theirselves
 *///ww w .ja v a2s .c  om
public static void notifyAppWidgets(Context context) {
    // Home widgets
    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    int[] ids = mgr.getAppWidgetIds(new ComponentName(context, ListWidgetProvider.class));
    Log.d(Constants.TAG, "Notifies AppWidget data changed for widgets " + Arrays.toString(ids));
    mgr.notifyAppWidgetViewDataChanged(ids, R.id.widget_list);

    // Dashclock
    LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(Constants.INTENT_UPDATE_DASHCLOCK));
}

From source file:de.hero.vertretungsplan.HtmlWork.java

@SuppressLint("NewApi")
private void updateWidget(boolean before) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
        AppWidgetManager appManager = AppWidgetManager.getInstance(context);
        ComponentName name = new ComponentName(context, WidgetProvider.class);
        if (before) {
            widget.setImageViewResource(R.id.aktButton, R.drawable.ic_action_aktualisieren_pressed);
        } else {//  w w w  .j  a va  2 s . c om
            widget.setImageViewResource(R.id.aktButton, R.drawable.aktualisieren_drawable);
            if (dataChanged) {
                appManager.notifyAppWidgetViewDataChanged(appManager.getAppWidgetIds(name), R.id.words);
            }
        }
        appManager.partiallyUpdateAppWidget(appManager.getAppWidgetIds(name), widget);
    }
}

From source file:org.gnucash.android.ui.widget.WidgetConfigurationActivity.java

/**
 * Updates the widget with id <code>appWidgetId</code> with information from the 
 * account with record ID <code>accountId</code>
  * If the account has been deleted, then a notice is posted in the widget
  * @param appWidgetId ID of the widget to be updated
  * @param accountId Database ID of the account tied to the widget
 *//*from   ww  w.j  a va2  s.c  o m*/
public static void updateWidget(Context context, int appWidgetId, long accountId) {
    Log.i("WidgetConfiguration", "Updating widget: " + appWidgetId);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(context);
    Account account = accountsDbAdapter.getAccount(accountId);

    if (account == null) {
        Log.i("WidgetConfiguration", "Account not found, resetting widget " + appWidgetId);
        //if account has been deleted, let the user know
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
        views.setTextViewText(R.id.account_name, context.getString(R.string.toast_account_deleted));
        views.setTextViewText(R.id.transactions_summary, "");
        //set it to simply open the app
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, AccountsActivity.class), 0);
        views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
        views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
        Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.remove(UxArgument.SELECTED_ACCOUNT_ID + appWidgetId);
        editor.commit();
        return;
    }

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
    views.setTextViewText(R.id.account_name, account.getName());
    Money accountBalance = accountsDbAdapter.getAccountBalance(accountId);

    views.setTextViewText(R.id.transactions_summary, accountBalance.formattedString(Locale.getDefault()));
    int color = account.getBalance().isNegative() ? R.color.debit_red : R.color.credit_green;
    views.setTextColor(R.id.transactions_summary, context.getResources().getColor(color));

    Intent accountViewIntent = new Intent(context, TransactionsActivity.class);
    accountViewIntent.setAction(Intent.ACTION_VIEW);
    accountViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    accountViewIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, accountId);
    PendingIntent accountPendingIntent = PendingIntent.getActivity(context, appWidgetId, accountViewIntent, 0);
    views.setOnClickPendingIntent(R.id.widget_layout, accountPendingIntent);

    Intent newTransactionIntent = new Intent(context, TransactionsActivity.class);
    newTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
    newTransactionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    newTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, accountId);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, newTransactionIntent, 0);
    views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:com.QuarkLabs.BTCeClient.UpdateWidgetsTask.java

@Override
protected void onPostExecute(JSONObject jsonObject) {
    if (jsonObject != null) {
        try {/*  w w w. j ava  2 s  .  co m*/
            Context context = mContext.get();
            if (context == null) {
                return;
            }
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            for (int x : mMap.keySet()) {
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
                double price = jsonObject.getJSONObject(mMap.get(x).replace("/", "_").toLowerCase(Locale.US))
                        .getDouble("last");
                String priceString;
                if (price > 1) {
                    priceString = (new DecimalFormat("#.##")).format(price);
                } else {
                    priceString = String.valueOf(price);
                }
                views.setTextViewText(R.id.widgetCurrencyValue, priceString);
                views.setTextViewText(R.id.widgetPair, mMap.get(x));
                String color = jsonObject.getJSONObject(mMap.get(x).replace("/", "_").toLowerCase(Locale.US))
                        .getString("color");
                int colorValue = color.equals("green") ? Color.GREEN : Color.RED;
                views.setTextColor(R.id.widgetCurrencyValue, colorValue);
                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                Bundle bundle = new Bundle();
                bundle.putIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                        appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class)));
                intent.putExtras(bundle);
                PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                views.setOnClickPendingIntent(R.id.widgetContainer, pi);
                SimpleDateFormat df = new SimpleDateFormat("EEE HH:mm", Locale.US);
                Calendar calendar = Calendar.getInstance();
                views.setTextViewText(R.id.widgetDate, df.format(calendar.getTime()));
                appWidgetManager.updateAppWidget(x, views);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}