Guest User

Untitled

a guest
Jun 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. public class IngredientsWidgetConfigureActivity extends Activity implements RecipesAdapter.RecipesAdapterOnClickHandler {
  2.  
  3.  
  4. int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
  5. RecipesAdapter mAdapter;
  6.  
  7.  
  8. public IngredientsWidgetConfigureActivity(){
  9. super();
  10. }
  11.  
  12. //save recipeId into SharedPrefs
  13. static void saveSelectedRecipe (Context context, int appWidgetId, int selectedRecipe){
  14. SharedPreferences.Editor prefs = context.getSharedPreferences(AppConstants.PREFS_NAME, 0).edit();
  15. prefs.putInt(AppConstants.PREF_PREFIX_KEY + appWidgetId, selectedRecipe);
  16. prefs.apply();
  17. }
  18.  
  19.  
  20. @Override
  21. protected void onCreate(@Nullable Bundle icicle) {
  22. super.onCreate(icicle);
  23.  
  24. setResult(RESULT_CANCELED);
  25.  
  26. setContentView(R.layout.ingredients_widget_configure);
  27.  
  28. //setup RecyclerView
  29. ...
  30.  
  31. Intent intent = getIntent();
  32. Bundle extras = intent.getExtras();
  33.  
  34. if (extras != null){
  35. mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
  36. }
  37. if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
  38. finish();
  39. return;
  40. }
  41.  
  42. }
  43.  
  44. @Override
  45. public void onClick(Recipe recipe) {
  46. final Context context = IngredientsWidgetConfigureActivity.this;
  47.  
  48. // When the button is clicked, store the recipe Id
  49. int selectedRecipe = recipe.id;
  50. saveSelectedRecipe(context, mAppWidgetId, selectedRecipe);
  51.  
  52. // It is the responsibility of the configuration activity to update the app widget
  53. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  54. IngredientsWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
  55.  
  56. // Make sure we pass back the original appWidgetId
  57. Intent resultValue = new Intent();
  58. resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
  59. setResult(RESULT_OK, resultValue);
  60. finish();
  61. }
  62.  
  63. ....
  64. static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager,
  65. int appWidgetId) {
  66.  
  67. RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ingredients_widget_layout);
  68.  
  69. Intent intent = new Intent(context, WidgetRemoteViewsService.class);
  70. intent.putExtra(AppConstants.APP_WIDGET_ID_KEY, appWidgetId);
  71. views.setRemoteAdapter(R.id.widget_list_view, intent);
  72.  
  73.  
  74. //TODO: recipes are not changing in different widgets
  75.  
  76. // Instruct the widget manager to update the widget
  77. appWidgetManager.updateAppWidget(appWidgetId, views);
  78. }
  79.  
  80.  
  81. @Override
  82. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  83.  
  84. // There may be multiple widgets active, so update all of them
  85. for (int appWidgetId : appWidgetIds) {
  86.  
  87. updateAppWidget(context, appWidgetManager, appWidgetId);
  88.  
  89.  
  90. }
  91.  
  92. }
  93.  
  94. public WidgetRemoteViewsFactory (Context context, Intent intent){
  95. mContext = context;
  96. appWidgetId = intent.getIntExtra(AppConstants.APP_WIDGET_ID_KEY, 1);
  97. }
  98.  
  99. @Override
  100. public void onCreate() {
  101.  
  102. }
  103.  
  104. @Override
  105. public void onDataSetChanged() {
  106.  
  107. final long identityToken = Binder.clearCallingIdentity();
  108.  
  109. //get id from SharedPref
  110. RecipeDatabase mDb = RecipeDatabase.getInstance(mContext);
  111. SharedPreferences prefs = mContext.getSharedPreferences(AppConstants.PREFS_NAME, 0);
  112. recipeId = prefs.getInt(AppConstants.PREF_PREFIX_KEY + appWidgetId, 9999);
  113.  
  114.  
  115. recipe = mDb.recipeDAO().loadRecipe(recipeId);
  116.  
  117.  
  118. Binder.restoreCallingIdentity(identityToken);
  119.  
  120.  
  121. }
  122.  
  123. @Override
  124. public void onDestroy() {
  125.  
  126. }
  127.  
  128. @Override
  129. public int getCount() {
  130. if (recipe == null) {
  131. return 0;
  132. } else {
  133. return recipe.ingredients.size();
  134. }
  135. }
  136.  
  137. @Override
  138. public RemoteViews getViewAt(int position) {
  139. if (position > recipe.ingredients.size()){
  140. return null;
  141. }
  142.  
  143.  
  144. RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);
  145. remoteViews.setTextViewText(R.id.widget_text, recipe.ingredients.get(position).getIngredient());
  146.  
  147. return remoteViews;
  148. }
  149.  
  150. @Override
  151. public RemoteViews getLoadingView() {
  152. return null;
  153. }
  154.  
  155. @Override
  156. public int getViewTypeCount() {
  157. return 1;
  158. }
  159.  
  160. @Override
  161. public long getItemId(int position) {
  162. return position;
  163. }
  164.  
  165. @Override
  166. public boolean hasStableIds() {
  167. return true;
  168. }
Add Comment
Please, Sign In to add comment