List of usage examples for android.appwidget AppWidgetManager updateAppWidget
public void updateAppWidget(ComponentName provider, RemoteViews views)
From source file:com.xengar.android.stocktracker.widget.ListWidgetProvider.java
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Perform this loop procedure for each App Widget that belongs to this provider for (int appWidgetId : appWidgetIds) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_list); // Create an Intent to launch MainActivity Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.widget, pendingIntent); // Set up the collection if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { setRemoteAdapter(context, views); } else {//from w w w.j ava 2 s . c om setRemoteAdapterV11(context, views); } boolean useDetailActivity = context.getResources().getBoolean(R.bool.use_detail_activity); Intent clickIntentTemplate = useDetailActivity ? new Intent(context, DetailActivity.class) : new Intent(context, MainActivity.class); PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context) .addNextIntentWithParentStack(clickIntentTemplate) .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate); views.setEmptyView(R.id.widget_list, R.id.widget_empty); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } }
From source file:com.abhijitvalluri.android.fitnotifications.widget.ServiceToggle.java
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Perform this loop procedure for each App Widget that belongs to this provider for (int appWidgetId : appWidgetIds) { // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.service_toggle_widget); if (NLService.isEnabled()) { views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_white_48dp); views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_green); views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_on_text)); views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.green)); } else {// w ww.j av a2 s. c om views.setImageViewResource(R.id.widgetToggleButton, R.drawable.ic_speaker_notes_off_white_48dp); views.setInt(R.id.widgetToggleButton, "setBackgroundResource", R.drawable.round_rectangle_red); views.setTextViewText(R.id.widgetToggleText, context.getString(R.string.widget_off_text)); views.setTextColor(R.id.widgetToggleText, ContextCompat.getColor(context, R.color.red)); } views.setOnClickPendingIntent(R.id.widgetToggleButton, getPendingSelfIntent(context, appWidgetId, TOGGLE_CLICKED)); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } }
From source file:ru.kaefik.isaifutdinov.an_weather_widget.AnWeatherWidget.java
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) throws JSONException { Log.i(TAG_SERVICE, "start updateAppWidget" + " appWidgetId = " + String.valueOf(appWidgetId)); String nameCity = ConfigActivity.loadStringParametersFromFile(context, String.valueOf(appWidgetId)); CityModel mCityModel = new CityModel(nameCity); // ? /* w w w . java2 s . co m*/ mCityModel = GetWeatherCityService.restoreCityInfoFromFile(context, mCityModel); Log.i(TAG_SERVICE, "updateAppWidget -> ?? " + mCityModel.getName() + " -> " + mCityModel.getTemp()); if (!nameCity.trim().equals("")) { Log.i(TAG_SERVICE, " updateAppWidget - >: " + nameCity + " appWidgetId = " + String.valueOf(appWidgetId)); // ? CLICK_WIDGET_BUTTON onReceive RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); // Intent ? Broadcast Intent active = new Intent(context, AnWeatherWidget.class); active.setAction(CLICK_WIDGET_BUTTON); //? ? PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); //? ? remoteViews.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent); //? remoteViews.setTextViewText(R.id.cityNameText, mCityModel.getName()); remoteViews.setTextViewText(R.id.tempCityText, mCityModel.getTemp() + "C"); remoteViews.setTextViewText(R.id.windText, Utils.windGradus2Rumb(mCityModel.getWinddirection()) + " (" + Float.toString(mCityModel.getWindspeed()) + " /?)"); remoteViews.setTextViewText(R.id.timeRefreshText, mCityModel.getTimeRefresh()); remoteViews.setTextViewText(R.id.descriptionWeatherText, mCityModel.getWeather("description")); remoteViews.setImageViewUri(R.id.weatherImageView, Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/" + "weather" + mCityModel.getWeather("icon"))); appWidgetManager.updateAppWidget(appWidgetId, remoteViews); // END - ? CLICK_WIDGET_BUTTON onReceive startGetWeatherCityService(context, appWidgetId, mCityModel); } else { Log.i(TAG_SERVICE, " updateAppWidget - > ? " + " appWidgetId = " + String.valueOf(appWidgetId)); } }
From source file:com.example.olivi.maphap.widget.DetailWidgetProvider.java
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //Start up DetailWidgetIntentService to query for regions data and see when it was last // updated. If the data is too old, the service will update it using MapHapService. Intent i = new Intent(context, DetailWidgetIntentService.class); context.startService(i);//from w w w . j a va 2 s. c o m // Perform this loop procedure for each App Widget that belongs to this provider for (int appWidgetId : appWidgetIds) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_detail); // Create an Intent to launch MainActivity Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.widget_detail, pendingIntent); // Set up the collection if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { setRemoteAdapter(context, views); } else { setRemoteAdapterV11(context, views); } Intent clickIntentTemplate = new Intent(context, DetailActivity.class); PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context) .addNextIntentWithParentStack(clickIntentTemplate) .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); views.setPendingIntentTemplate(R.id.widget_list, clickPendingIntentTemplate); views.setEmptyView(R.id.widget_list, R.id.widget_empty); // Tell the AppWidgetManager to perform an update on the current app com.example // .olivi.maphap.widget appWidgetManager.updateAppWidget(appWidgetId, views); } }
From source file:org.zoumbox.mh_dla_notifier.Receiver.java
protected void checkForWidgetsUpdate(Context context, Troll troll) { try {//from ww w . j a va 2 s . c om AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); ComponentName componentName = new ComponentName(context, HomeScreenWidget.class); int[] appWidgetIds = widgetManager.getAppWidgetIds(componentName); if (appWidgetIds != null && appWidgetIds.length > 0) { // FIXME AThimel 14/02/14 Remove ASAP StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); String dlaText = Trolls.getWidgetDlaTextFunction(context).apply(troll); Bitmap blason = MhDlaNotifierUtils.loadBlasonForWidget(troll.getBlason(), context.getCacheDir()); for (int appWidgetId : appWidgetIds) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.home_screen_widget); views.setTextViewText(R.id.widgetDla, dlaText); if (blason == null) { views.setImageViewResource(R.id.widgetImage, R.drawable.trarnoll_square_transparent_128); } else { views.setImageViewBitmap(R.id.widgetImage, blason); } // Tell the AppWidgetManager to perform an update on the current app widget widgetManager.updateAppWidget(appWidgetId, views); } } } catch (Exception eee) { Log.e(TAG, "Unable to update widget(s)", eee); } }
From source file:org.solovyev.android.calculator.widget.CalculatorWidget.java
private void updateWidget(@Nonnull Context context, @Nonnull AppWidgetManager manager, @Nonnull int[] widgetIds, boolean partially) { final EditorState editorState = editor.getState(); final DisplayState displayState = display.getState(); final Resources resources = context.getResources(); final SimpleTheme theme = App.getWidgetTheme().resolveThemeFor(App.getTheme()); for (int widgetId : widgetIds) { final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout(manager, widgetId, resources, theme)); if (!partially || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { for (CppButton button : CppButton.values()) { final PendingIntent intent = intents.get(context, button); if (intent != null) { final int buttonId; if (button == CppButton.settings_widget) { // overriding default settings button behavior buttonId = CppButton.settings.id; } else { buttonId = button.id; }// w w w. j av a 2 s . c o m views.setOnClickPendingIntent(buttonId, intent); } } } updateEditorState(context, views, editorState, theme); updateDisplayState(context, views, displayState, theme); views.setTextViewText(R.id.cpp_button_multiplication, engine.getMultiplicationSign()); if (partially && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { manager.partiallyUpdateAppWidget(widgetId, views); } else { manager.updateAppWidget(widgetId, views); } } }
From source file:com.QuarkLabs.BTCeClient.UpdateWidgetsTask.java
@Override protected void onPostExecute(JSONObject jsonObject) { if (jsonObject != null) { try {//from w ww .j ava2 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(); } } }
From source file:ru.kaefik.isaifutdinov.an_weather_widget.AnWeatherWidget.java
@Override public void onReceive(Context context, Intent intent) { Log.i(TAG_SERVICE, " onReceive " + intent.getAction()); super.onReceive(context, intent); if (FORCE_WIDGET_UPDATE.equals(intent.getAction())) { String nameCity = intent.getStringExtra(PARAM_CITY); String tempCity = intent.getStringExtra(PARAM_TEMP); String windCity = intent.getStringExtra(PARAM_WIND); String timeRefreshCity = intent.getStringExtra(PARAM_TIMEREFRESH); String weatherImageCity = intent.getStringExtra(PARAM_WEATHERIMAGE); String descriptionWeather = intent.getStringExtra(PARAM_DESCWEATHER); int WidgetId = intent.getIntExtra(PARAM_WIDGETID, 0); //TODO: ? , ?. ? ? GetWeatherCityService Log.i(TAG_SERVICE, "onReceive " + nameCity + " -> " + tempCity); // /*from ww w . j av a2s. co m*/ AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class); int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget); for (int i = 0; i < appWidgetId.length; i++) { Log.i(TAG_SERVICE, "onReceive -> appWidgetId = " + String.valueOf(appWidgetId[i])); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); if (WidgetId == appWidgetId[i]) { views.setTextViewText(R.id.cityNameText, nameCity); views.setTextViewText(R.id.tempCityText, tempCity); views.setTextViewText(R.id.windText, windCity); views.setTextViewText(R.id.timeRefreshText, timeRefreshCity); views.setTextViewText(R.id.descriptionWeatherText, descriptionWeather); views.setImageViewUri(R.id.weatherImageView, Uri.parse("android.resource://ru.kaefik.isaifutdinov.an_weather_widget/mipmap/" + "weather" + weatherImageCity)); // ? CLICK_WIDGET_BUTTON onReceive // Intent ? Broadcast Intent active = new Intent(context, AnWeatherWidget.class); active.setAction(CLICK_WIDGET_BUTTON); //? ? PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); //? ? views.setOnClickPendingIntent(R.id.refreshButton, actionPendingIntent); //? // END - ? CLICK_WIDGET_BUTTON onReceive appWidgetManager.updateAppWidget(appWidgetId[i], views); break; } appWidgetManager.updateAppWidget(appWidgetId[i], views); } } if (CLICK_WIDGET_BUTTON.equals(intent.getAction())) { Log.i(TAG_SERVICE, " "); // AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisWidget = new ComponentName(context, AnWeatherWidget.class); int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.an_weather_widget); // ? for (int i = 0; i < appWidgetId.length; i++) { Log.i(TAG_SERVICE, "id -> " + String.valueOf(appWidgetId[i])); String nameCity = ConfigActivity.loadStringParametersFromFile(context, String.valueOf(appWidgetId[i])); startGetWeatherCityService(context, appWidgetId[i], new CityModel(nameCity)); } } }
From source file:com.eugene.fithealthmaingit.UI.NavFragments.FragmentJournalMainHome.java
/** * Equation for all of the Nutrition and Meal information */// w w w .j a v a2s . c om private void equations() { double mCalorieGoal = Double.valueOf(sharedPreferences.getString(Globals.USER_CALORIES_TO_REACH_GOAL, "")); double mFatGoal = Double.valueOf(sharedPreferences.getString(Globals.USER_DAILY_FAT, "")); double mCarbGoal = Double.valueOf(sharedPreferences.getString(Globals.USER_DAILY_CARBOHYDRATES, "")); double mProteinGoal = Double.valueOf(sharedPreferences.getString(Globals.USER_DAILY_PROTEIN, "")); mCalorieGoalMeal = Double.valueOf(sharedPreferences.getString(Globals.USER_CALORIES_TO_REACH_GOAL, "")) / 4; icSnack = (ImageView) v.findViewById(R.id.icSnack); icBreakfast = (ImageView) v.findViewById(R.id.icBreakfast); icLunch = (ImageView) v.findViewById(R.id.icLunch); icDinner = (ImageView) v.findViewById(R.id.icDinner); // _________________________Calories Snack_____________________________ double mCalConsumedSnack = 0; for (LogMeal logMeal : mLogSnackAdapter.getLogs()) { mCalConsumedSnack += logMeal.getCalorieCount(); } mCalSnack.setText(df.format(mCalConsumedSnack)); // Set icon visible and color based on calories consumed for meal. if (mCalConsumedSnack >= mCalorieGoalMeal + 100) { icSnack.setImageResource(R.mipmap.ic_check_circle); icSnack.setColorFilter(Color.parseColor("#F44336"), android.graphics.PorterDuff.Mode.MULTIPLY); } else if (mCalConsumedSnack > mCalorieGoalMeal - 100 && mCalConsumedSnack < mCalorieGoalMeal + 99) { icSnack.setImageResource(R.mipmap.ic_check_circle); icSnack.setColorFilter(Color.parseColor("#4CAF50"), android.graphics.PorterDuff.Mode.MULTIPLY); } else { icSnack.setImageResource(R.mipmap.ic_check); icSnack.setColorFilter(Color.parseColor("#6D6D6D"), android.graphics.PorterDuff.Mode.MULTIPLY); } // _________________________Calories Breakfast_____________________________ double mCalConsumedBreakfast = 0; for (LogMeal logMeal : mLogBreakfastAdapter.getLogs()) { mCalConsumedBreakfast += logMeal.getCalorieCount(); } mCalBreakfast.setText(df.format(mCalConsumedBreakfast)); // Set icon visible and color based on calories consumed for meal. if (mCalConsumedBreakfast >= mCalorieGoalMeal + 100) { icBreakfast.setColorFilter(Color.parseColor("#F44336"), android.graphics.PorterDuff.Mode.MULTIPLY); icBreakfast.setImageResource(R.mipmap.ic_check_circle); } else if (mCalConsumedBreakfast > mCalorieGoalMeal - 100 && mCalConsumedBreakfast < mCalorieGoalMeal + 99) { icBreakfast.setColorFilter(Color.parseColor("#4CAF50"), android.graphics.PorterDuff.Mode.MULTIPLY); icBreakfast.setImageResource(R.mipmap.ic_check_circle); } else { icBreakfast.setImageResource(R.mipmap.ic_check); icBreakfast.setColorFilter(Color.parseColor("#6D6D6D"), android.graphics.PorterDuff.Mode.MULTIPLY); } // _________________________Calories Lunch_____________________________ double mCalConsumedLunch = 0; for (LogMeal logMeal : mLogLunchAdapter.getLogs()) { mCalConsumedLunch += logMeal.getCalorieCount(); } mCalLunch.setText(df.format(mCalConsumedLunch)); // Set icon visible and color based on calories consumed for meal. if (mCalConsumedLunch >= mCalorieGoalMeal + 100) { icLunch.setImageResource(R.mipmap.ic_check_circle); icLunch.setColorFilter(Color.parseColor("#F44336"), android.graphics.PorterDuff.Mode.MULTIPLY); } else if (mCalConsumedLunch > mCalorieGoalMeal - 100 && mCalConsumedLunch < mCalorieGoalMeal + 99) { icLunch.setImageResource(R.mipmap.ic_check_circle); icLunch.setColorFilter(Color.parseColor("#4CAF50"), android.graphics.PorterDuff.Mode.MULTIPLY); } else { icLunch.setImageResource(R.mipmap.ic_check); icLunch.setColorFilter(Color.parseColor("#6D6D6D"), android.graphics.PorterDuff.Mode.MULTIPLY); } // _________________________Calories Lunch_____________________________ double mCalConsumedDinner = 0; for (LogMeal logMeal : mLogDinnerAdapter.getLogs()) { mCalConsumedDinner += logMeal.getCalorieCount(); } mCalDinner.setText(df.format(mCalConsumedDinner)); // Set icon visible and color based on calories consumed for meal. if (mCalConsumedDinner >= mCalorieGoalMeal + 100) { icDinner.setImageResource(R.mipmap.ic_check_circle); icDinner.setColorFilter(Color.parseColor("#F44336"), android.graphics.PorterDuff.Mode.MULTIPLY); } else if (mCalConsumedDinner > mCalorieGoalMeal - 100 && mCalConsumedDinner < mCalorieGoalMeal + 99) { icDinner.setImageResource(R.mipmap.ic_check_circle); icDinner.setColorFilter(Color.parseColor("#4CAF50"), android.graphics.PorterDuff.Mode.MULTIPLY); } else { icDinner.setImageResource(R.mipmap.ic_check); icDinner.setColorFilter(Color.parseColor("#6D6D6D"), android.graphics.PorterDuff.Mode.MULTIPLY); } // _________________________Calories, Fat, Carbs, Protein All_____________________________ // Nutrition Consumed double mAllCaloriesConsumed = 0; double mAllFatConsumed = 0; double mAllCarbsConsumed = 0; double mAllProteinConsumed = 0; for (LogMeal logMeal : mLogAdapterAll.getLogs()) { mAllCaloriesConsumed += logMeal.getCalorieCount(); mAllFatConsumed += logMeal.getFatCount(); mAllCarbsConsumed += logMeal.getCarbCount(); mAllProteinConsumed += logMeal.getProteinCount(); } // Nutrition Goals // Remainder Nutrition mCaloriesRemainder.setText(df.format(mCalorieGoal - mAllCaloriesConsumed) + " Left"); mFatRemainder.setText(df.format(mFatGoal - mAllFatConsumed) + " Left"); mCarbRemainder.setText(df.format(mCarbGoal - mAllCarbsConsumed) + " Left"); mProteinRemainder.setText(df.format(mProteinGoal - mAllProteinConsumed) + " Left"); // Progress bars mPbCalories.setMax(Integer.valueOf(df.format(mCalorieGoal))); mPbCalories.setProgress(Integer.valueOf(df.format(mAllCaloriesConsumed))); mPbFat.setMax(Integer.valueOf(df.format(mFatGoal))); mPbFat.setProgress(Integer.valueOf(df.format(mAllFatConsumed))); mPbCarbs.setMax(Integer.valueOf(df.format(mCarbGoal))); mPbCarbs.setProgress(Integer.valueOf(df.format(mAllCarbsConsumed))); mPbProtein.setMax(Integer.valueOf(df.format(mProteinGoal))); mPbProtein.setProgress(Integer.valueOf(df.format(mAllProteinConsumed))); /** * Update Widget */ Context context = getActivity(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); if (appWidgetManager != null) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); ComponentName thisWidget = new ComponentName(context, FitHealthWidget.class); views.setProgressBar(R.id.pbCal, Integer.valueOf(df.format(mCalorieGoal)), Integer.valueOf(df.format(mAllCaloriesConsumed)), false); views.setProgressBar(R.id.pbFat, Integer.valueOf(df.format(mFatGoal)), Integer.valueOf(df.format(mAllFatConsumed)), false); views.setProgressBar(R.id.pbCarb, Integer.valueOf(df.format(mCarbGoal)), Integer.valueOf(df.format(mAllCarbsConsumed)), false); views.setProgressBar(R.id.pbPro, Integer.valueOf(df.format(mProteinGoal)), Integer.valueOf(df.format(mAllProteinConsumed)), false); views.setTextViewText(R.id.txtRemainderCal, df.format(mCalorieGoal - mAllCaloriesConsumed)); views.setTextViewText(R.id.txtRemainderFat, df.format(mFatGoal - mAllFatConsumed)); views.setTextViewText(R.id.txtRemainderCarb, df.format(mCarbGoal - mAllCarbsConsumed)); views.setTextViewText(R.id.txtRemainderPro, df.format(mProteinGoal - mAllProteinConsumed)); appWidgetManager.updateAppWidget(thisWidget, views); } }
From source file:eu.power_switch.widget.activity.ConfigureReceiverWidgetActivity.java
private void saveCurrentConfiguration() { try {//from w w w. j a v a 2 s. com // First, get the App Widget ID from the Intent that launched the Activity: Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) { int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); // Perform your App Widget configuration: Apartment selectedApartment = getSelectedApartment(); Room selectedRoom = selectedApartment.getRoom(spinnerRoom.getSelectedItem().toString()); Receiver selectedReceiver = selectedRoom.getReceiver(spinnerReceiver.getSelectedItem().toString()); // save new widget data to database ReceiverWidget receiverWidget = new ReceiverWidget(appWidgetId, selectedRoom.getId(), selectedReceiver.getId()); DatabaseHandler.addReceiverWidget(receiverWidget); // When the configuration is complete, get an instance of // the AppWidgetManager by calling getInstance(Context): AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(ConfigureReceiverWidgetActivity.this); // Update the App Widget with a RemoteViews layout by // calling updateAppWidget(int, RemoteViews): RemoteViews remoteViews = new RemoteViews(getString(eu.power_switch.shared.R.string.PACKAGE_NAME), R.layout.widget_receiver); LinkedList<Button> buttons = selectedReceiver.getButtons(); remoteViews.setTextViewText(R.id.textView_receiver_widget_name, selectedApartment.getName() + ": " + selectedRoom.getName() + ": " + selectedReceiver.getName()); int buttonOffset = 0; for (Button button : buttons) { // set button action RemoteViews buttonView = new RemoteViews( getString(eu.power_switch.shared.R.string.PACKAGE_NAME), R.layout.widget_receiver_button_layout); SpannableString s = new SpannableString(button.getName()); s.setSpan(new StyleSpan(Typeface.BOLD), 0, button.getName().length(), 0); buttonView.setTextViewText(R.id.button_widget_universal, s); if (SmartphonePreferencesHandler.getHighlightLastActivatedButton() && selectedReceiver.getLastActivatedButtonId().equals(button.getId())) { buttonView.setTextColor(R.id.button_widget_universal, ContextCompat.getColor(getApplicationContext(), R.color.color_light_blue_a700)); } PendingIntent pendingIntent = WidgetIntentReceiver.buildReceiverWidgetActionPendingIntent( getApplicationContext(), selectedApartment, selectedRoom, selectedReceiver, button, appWidgetId * 15 + buttonOffset); buttonView.setOnClickPendingIntent(R.id.button_widget_universal, pendingIntent); remoteViews.addView(R.id.linearlayout_receiver_widget, buttonView); buttonOffset++; } appWidgetManager.updateAppWidget(appWidgetId, remoteViews); // Finally, create the return Intent, set it with the // Activity result, and finish the Activity: Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); setResult(RESULT_OK, resultValue); finish(); } } catch (Exception e) { StatusMessageHandler.showErrorMessage(this, e); } }