OSDN Git Service

Always specify forHomeScreen parameter for U.canDrawOverlays()
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / ui / DashboardController.java
1 /* Based on code by Leonardo Fischer
2  * See https://github.com/lgfischer/WidgetHostExample
3  *
4  * Copyright 2016 Braden Farmer
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package com.farmerbb.taskbar.ui;
20
21 import android.animation.Animator;
22 import android.animation.AnimatorListenerAdapter;
23 import android.annotation.SuppressLint;
24 import android.annotation.TargetApi;
25 import android.appwidget.AppWidgetHost;
26 import android.appwidget.AppWidgetHostView;
27 import android.appwidget.AppWidgetManager;
28 import android.appwidget.AppWidgetProviderInfo;
29 import android.content.BroadcastReceiver;
30 import android.content.ComponentName;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.content.SharedPreferences;
35 import android.content.pm.PackageManager;
36 import android.content.res.Configuration;
37 import android.graphics.Color;
38 import android.graphics.ColorMatrix;
39 import android.graphics.ColorMatrixColorFilter;
40 import android.graphics.drawable.Drawable;
41 import android.os.Build;
42 import android.os.Bundle;
43 import android.os.Handler;
44 import android.os.Process;
45 import android.support.v4.content.LocalBroadcastManager;
46 import android.support.v4.graphics.ColorUtils;
47 import android.util.SparseArray;
48 import android.view.MotionEvent;
49 import android.view.PointerIcon;
50 import android.view.View;
51 import android.view.ViewGroup;
52 import android.view.WindowManager;
53 import android.widget.FrameLayout;
54 import android.widget.ImageView;
55 import android.widget.LinearLayout;
56 import android.widget.TextView;
57 import android.widget.Toast;
58
59 import com.farmerbb.taskbar.R;
60 import com.farmerbb.taskbar.activity.DashboardActivity;
61 import com.farmerbb.taskbar.activity.HomeActivityDelegate;
62 import com.farmerbb.taskbar.activity.dark.DashboardActivityDark;
63 import com.farmerbb.taskbar.util.DashboardHelper;
64 import com.farmerbb.taskbar.widget.DashboardCell;
65 import com.farmerbb.taskbar.util.FreeformHackHelper;
66 import com.farmerbb.taskbar.util.LauncherHelper;
67 import com.farmerbb.taskbar.util.U;
68
69 import java.util.List;
70
71 public class DashboardController implements Controller {
72
73     private AppWidgetManager mAppWidgetManager;
74     private AppWidgetHost mAppWidgetHost;
75
76     private Context context;
77     private LinearLayout layout;
78
79     private SparseArray<DashboardCell> cells = new SparseArray<>();
80     private SparseArray<AppWidgetHostView> widgets = new SparseArray<>();
81
82     private final int APPWIDGET_HOST_ID = 123;
83
84     private int columns;
85     private int rows;
86     private int maxSize;
87     private int previouslySelectedCell = -1;
88
89     private View.OnClickListener ocl = view -> toggleDashboard();
90
91     private View.OnClickListener cellOcl = view -> cellClick(view, true);
92
93     private View.OnHoverListener cellOhl = (v, event) -> {
94         cellClick(v, false);
95
96         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
97             v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
98
99         return false;
100     };
101
102     private View.OnLongClickListener olcl = v -> {
103         cellLongClick(v);
104         return true;
105     };
106
107     private View.OnGenericMotionListener ogml = (view, motionEvent) -> {
108         if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
109                 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
110             cellLongClick(view);
111
112         return false;
113     };
114
115     private DashboardCell.OnInterceptedLongPressListener listener = this::cellLongClick;
116
117     private BroadcastReceiver toggleReceiver = new BroadcastReceiver() {
118         @Override
119         public void onReceive(Context context, Intent intent) {
120             toggleDashboard();
121         }
122     };
123
124     private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
125         @Override
126         public void onReceive(Context context, Intent intent) {
127             fadeIn();
128
129             if(intent.hasExtra("appWidgetId") && intent.hasExtra("cellId")) {
130                 int appWidgetId = intent.getExtras().getInt("appWidgetId", -1);
131                 int cellId = intent.getExtras().getInt("cellId", -1);
132
133                 addWidget(appWidgetId, cellId, true);
134             }
135         }
136     };
137
138     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
139         @Override
140         public void onReceive(Context context, Intent intent) {
141             fadeIn();
142
143             if(intent.hasExtra("cellId")) {
144                 int cellId = intent.getExtras().getInt("cellId", -1);
145
146                 removeWidget(cellId, false);
147             }
148         }
149     };
150
151     private BroadcastReceiver hideReceiver = new BroadcastReceiver() {
152         @Override
153         public void onReceive(Context context, Intent intent) {
154             hideDashboard();
155         }
156     };
157
158     public DashboardController(Context context) {
159         this.context = context;
160     }
161
162     @TargetApi(Build.VERSION_CODES.M)
163     @Override
164     public void onCreateHost(Host host) {
165         SharedPreferences pref = U.getSharedPreferences(context);
166         if(pref.getBoolean("dashboard", false)) {
167             if(pref.getBoolean("taskbar_active", false) || LauncherHelper.getInstance().isOnHomeScreen()) {
168                 if(U.canDrawOverlays(context, host instanceof HomeActivityDelegate))
169                     drawDashboard(host);
170                 else {
171                     pref.edit().putBoolean("taskbar_active", false).apply();
172
173                     host.terminate();
174                 }
175             } else host.terminate();
176         } else host.terminate();
177     }
178
179     @SuppressLint("RtlHardcoded")
180     private void drawDashboard(Host host) {
181         final ViewParams params = new ViewParams(
182                 WindowManager.LayoutParams.MATCH_PARENT,
183                 WindowManager.LayoutParams.MATCH_PARENT,
184                 -1,
185                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
186         );
187
188         // Initialize views
189         layout = new LinearLayout(context);
190         layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
191         layout.setVisibility(View.GONE);
192         layout.setAlpha(0);
193
194         SharedPreferences pref = U.getSharedPreferences(context);
195         int width = pref.getInt("dashboard_width", context.getApplicationContext().getResources().getInteger(R.integer.dashboard_width));
196         int height = pref.getInt("dashboard_height", context.getApplicationContext().getResources().getInteger(R.integer.dashboard_height));
197
198         boolean isPortrait = context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
199         boolean isLandscape = context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
200
201         if(isPortrait) {
202             columns = height;
203             rows = width;
204         }
205
206         if(isLandscape) {
207             columns = width;
208             rows = height;
209         }
210
211         maxSize = columns * rows;
212
213         int backgroundTint = U.getBackgroundTint(context);
214         int accentColor = U.getAccentColor(context);
215         int accentColorAlt = accentColor;
216         accentColorAlt = ColorUtils.setAlphaComponent(accentColorAlt, Color.alpha(accentColorAlt) / 2);
217
218         int cellCount = 0;
219
220         for(int i = 0; i < columns; i++) {
221             LinearLayout layout2 = new LinearLayout(context);
222             layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
223             layout2.setOrientation(LinearLayout.VERTICAL);
224
225             for(int j = 0; j < rows; j++) {
226                 DashboardCell cellLayout = (DashboardCell) View.inflate(context, R.layout.dashboard, null);
227                 cellLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
228                 cellLayout.setBackgroundColor(backgroundTint);
229                 cellLayout.setOnClickListener(cellOcl);
230                 cellLayout.setOnHoverListener(cellOhl);
231
232                 TextView empty = cellLayout.findViewById(R.id.empty);
233                 empty.setBackgroundColor(accentColorAlt);
234                 empty.setTextColor(accentColor);
235
236                 Bundle bundle = new Bundle();
237                 bundle.putInt("cellId", cellCount);
238
239                 cellLayout.setTag(bundle);
240                 cells.put(cellCount, cellLayout);
241                 cellCount++;
242
243                 layout2.addView(cellLayout);
244             }
245
246             layout.addView(layout2);
247         }
248
249         mAppWidgetManager = AppWidgetManager.getInstance(context);
250         mAppWidgetHost = new AppWidgetHost(context, APPWIDGET_HOST_ID);
251         mAppWidgetHost.startListening();
252
253         for(int i = 0; i < maxSize; i++) {
254             int appWidgetId = pref.getInt("dashboard_widget_" + i, -1);
255             if(appWidgetId != -1)
256                 addWidget(appWidgetId, i, false);
257             else if(pref.getBoolean("dashboard_widget_" + i + "_placeholder", false))
258                 addPlaceholder(i);
259         }
260
261         mAppWidgetHost.stopListening();
262
263         LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
264
265         lbm.unregisterReceiver(toggleReceiver);
266         lbm.unregisterReceiver(addWidgetReceiver);
267         lbm.unregisterReceiver(removeWidgetReceiver);
268         lbm.unregisterReceiver(hideReceiver);
269
270         lbm.registerReceiver(toggleReceiver, new IntentFilter("com.farmerbb.taskbar.TOGGLE_DASHBOARD"));
271         lbm.registerReceiver(addWidgetReceiver, new IntentFilter("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED"));
272         lbm.registerReceiver(removeWidgetReceiver, new IntentFilter("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED"));
273         lbm.registerReceiver(hideReceiver, new IntentFilter("com.farmerbb.taskbar.HIDE_DASHBOARD"));
274
275         host.addView(layout, params);
276
277         new Handler().postDelayed(() -> {
278             int paddingSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size);
279
280             switch(U.getTaskbarPosition(context)) {
281                 case "top_vertical_left":
282                 case "bottom_vertical_left":
283                     layout.setPadding(paddingSize, 0, 0, 0);
284                     break;
285                 case "top_left":
286                 case "top_right":
287                     layout.setPadding(0, paddingSize, 0, 0);
288                     break;
289                 case "top_vertical_right":
290                 case "bottom_vertical_right":
291                     layout.setPadding(0, 0, paddingSize, 0);
292                     break;
293                 case "bottom_left":
294                 case "bottom_right":
295                     layout.setPadding(0, 0, 0, paddingSize);
296                     break;
297             }
298         }, 100);
299     }
300
301     private void toggleDashboard() {
302         if(layout.getVisibility() == View.GONE)
303             showDashboard();
304         else
305             hideDashboard();
306     }
307
308     @SuppressWarnings("deprecation")
309     @TargetApi(Build.VERSION_CODES.N)
310     private void showDashboard() {
311         if(layout.getVisibility() == View.GONE) {
312             layout.setOnClickListener(ocl);
313             fadeIn();
314
315             LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_APPEARING"));
316             LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
317
318             boolean inFreeformMode = FreeformHackHelper.getInstance().isInFreeformWorkspace();
319
320             final SharedPreferences pref = U.getSharedPreferences(context);
321             Intent intent = null;
322
323             switch(pref.getString("theme", "light")) {
324                 case "light":
325                     intent = new Intent(context, DashboardActivity.class);
326                     break;
327                 case "dark":
328                     intent = new Intent(context, DashboardActivityDark.class);
329                     break;
330             }
331
332             if(intent != null) {
333                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
334                 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
335             }
336
337             if(inFreeformMode) {
338                 if(intent != null && U.hasBrokenSetLaunchBoundsApi())
339                     intent.putExtra("context_menu_fix", true);
340
341                 U.startActivityMaximized(context, intent);
342             } else
343                 context.startActivity(intent);
344
345             for(int i = 0; i < maxSize; i++) {
346                 final DashboardCell cellLayout = cells.get(i);
347                 final AppWidgetHostView hostView = widgets.get(i);
348
349                 if(hostView != null) {
350                     try {
351                         context.getPackageManager().getApplicationInfo(hostView.getAppWidgetInfo().provider.getPackageName(), 0);
352                         hostView.post(() -> {
353                             ViewGroup.LayoutParams params = hostView.getLayoutParams();
354                             params.width = cellLayout.getWidth();
355                             params.height = cellLayout.getHeight();
356                             hostView.setLayoutParams(params);
357                             hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
358                         });
359                     } catch (PackageManager.NameNotFoundException e) {
360                         removeWidget(i, false);
361                     } catch (NullPointerException e) {
362                         removeWidget(i, true);
363                     }
364                 }
365             }
366
367             if(!pref.getBoolean("dashboard_tutorial_shown", false)) {
368                 U.showToastLong(context, R.string.dashboard_tutorial);
369                 pref.edit().putBoolean("dashboard_tutorial_shown", true).apply();
370             }
371         }
372     }
373
374     private void hideDashboard() {
375         if(layout.getVisibility() == View.VISIBLE) {
376             layout.setOnClickListener(null);
377             fadeOut(true);
378
379             for(int i = 0; i < maxSize; i++) {
380                 FrameLayout frameLayout = cells.get(i);
381                 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
382             }
383
384             previouslySelectedCell = -1;
385         }
386     }
387
388     private void fadeIn() {
389         mAppWidgetHost.startListening();
390
391         DashboardHelper.getInstance().setDashboardOpen(true);
392
393         layout.setVisibility(View.VISIBLE);
394         layout.animate()
395                 .alpha(1)
396                 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
397                 .setListener(null);
398     }
399
400     private void fadeOut(final boolean sendIntent) {
401         mAppWidgetHost.stopListening();
402
403         DashboardHelper.getInstance().setDashboardOpen(false);
404
405         layout.animate()
406                 .alpha(0)
407                 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
408                 .setListener(new AnimatorListenerAdapter() {
409                     @Override
410                     public void onAnimationEnd(Animator animation) {
411                         layout.setVisibility(View.GONE);
412                         if(sendIntent) LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING"));
413                     }
414                 });
415     }
416
417     @TargetApi(Build.VERSION_CODES.M)
418     @Override
419     public void onRecreateHost(Host host) {
420         if(layout != null) {
421             try {
422                 host.removeView(layout);
423             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
424
425             SharedPreferences pref = U.getSharedPreferences(context);
426             if(U.canDrawOverlays(context, host instanceof HomeActivityDelegate))
427                 drawDashboard(host);
428             else {
429                 pref.edit().putBoolean("taskbar_active", false).apply();
430
431                 host.terminate();
432             }
433         }
434     }
435
436     @Override
437     public void onDestroyHost(Host host) {
438         if(layout != null)
439             try {
440                 host.removeView(layout);
441             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
442
443         LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
444
445         lbm.unregisterReceiver(toggleReceiver);
446         lbm.unregisterReceiver(addWidgetReceiver);
447         lbm.unregisterReceiver(removeWidgetReceiver);
448         lbm.unregisterReceiver(hideReceiver);
449
450         lbm.sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING"));
451     }
452
453     private void cellClick(View view, boolean isActualClick) {
454         Bundle bundle = (Bundle) view.getTag();
455         int cellId = bundle.getInt("cellId");
456         int appWidgetId = bundle.getInt("appWidgetId", -1);
457
458         int currentlySelectedCell = appWidgetId == -1 ? cellId : -1;
459
460         SharedPreferences pref = U.getSharedPreferences(context);
461         boolean shouldShowPlaceholder = pref.getBoolean("dashboard_widget_" + cellId + "_placeholder", false);
462         if(isActualClick && ((appWidgetId == -1 && currentlySelectedCell == previouslySelectedCell) || shouldShowPlaceholder)) {
463             fadeOut(false);
464
465             FrameLayout frameLayout = cells.get(currentlySelectedCell);
466             frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
467
468             Intent intent = new Intent("com.farmerbb.taskbar.ADD_WIDGET_REQUESTED");
469             intent.putExtra("appWidgetId", APPWIDGET_HOST_ID);
470             intent.putExtra("cellId", cellId);
471             LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
472
473             if(shouldShowPlaceholder) {
474                 String providerName = pref.getString("dashboard_widget_" + cellId + "_provider", "null");
475                 if(!providerName.equals("null")) {
476                     ComponentName componentName = ComponentName.unflattenFromString(providerName);
477
478                     List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
479                     for(AppWidgetProviderInfo info : providerInfoList) {
480                         if(info.provider.equals(componentName)) {
481                             U.showToast(context, context.getString(R.string.widget_restore_toast, info.loadLabel(context.getPackageManager())), Toast.LENGTH_SHORT);
482                             break;
483                         }
484                     }
485                 }
486             }
487
488             previouslySelectedCell = -1;
489         } else {
490             for(int i = 0; i < maxSize; i++) {
491                 FrameLayout frameLayout = cells.get(i);
492                 frameLayout.findViewById(R.id.empty).setVisibility(i == currentlySelectedCell && !shouldShowPlaceholder ? View.VISIBLE : View.GONE);
493             }
494
495             previouslySelectedCell = currentlySelectedCell;
496         }
497     }
498
499     private void cellLongClick(View view) {
500         fadeOut(false);
501
502         Bundle bundle = (Bundle) view.getTag();
503         int cellId = bundle.getInt("cellId");
504
505         Intent intent = new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_REQUESTED");
506         intent.putExtra("cellId", cellId);
507         LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
508     }
509
510     private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
511         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
512
513         final DashboardCell cellLayout = cells.get(cellId);
514         final AppWidgetHostView hostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
515         hostView.setAppWidget(appWidgetId, appWidgetInfo);
516
517         Bundle bundle = new Bundle();
518         bundle.putInt("cellId", cellId);
519         hostView.setTag(bundle);
520
521         cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
522         cellLayout.findViewById(R.id.placeholder).setVisibility(View.GONE);
523         cellLayout.setOnLongClickListener(olcl);
524         cellLayout.setOnGenericMotionListener(ogml);
525         cellLayout.setOnInterceptedLongPressListener(listener);
526
527         LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
528         linearLayout.addView(hostView);
529
530         Bundle bundle2 = (Bundle) cellLayout.getTag();
531         bundle2.putInt("appWidgetId", appWidgetId);
532         cellLayout.setTag(bundle2);
533
534         widgets.put(cellId, hostView);
535
536         if(shouldSave) {
537             SharedPreferences pref = U.getSharedPreferences(context);
538             SharedPreferences.Editor editor = pref.edit();
539             editor.putInt("dashboard_widget_" + cellId, appWidgetId);
540             editor.putString("dashboard_widget_" + cellId + "_provider", appWidgetInfo.provider.flattenToString());
541             editor.remove("dashboard_widget_" + cellId + "_placeholder");
542             editor.apply();
543         }
544
545         new Handler().post(() -> {
546             ViewGroup.LayoutParams params = hostView.getLayoutParams();
547             params.width = cellLayout.getWidth();
548             params.height = cellLayout.getHeight();
549             hostView.setLayoutParams(params);
550             hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
551         });
552     }
553
554     private void removeWidget(int cellId, boolean tempRemove) {
555         widgets.remove(cellId);
556
557         DashboardCell cellLayout = cells.get(cellId);
558         Bundle bundle = (Bundle) cellLayout.getTag();
559
560         mAppWidgetHost.deleteAppWidgetId(bundle.getInt("appWidgetId"));
561         bundle.remove("appWidgetId");
562
563         LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
564         linearLayout.removeAllViews();
565
566         cellLayout.setTag(bundle);
567         cellLayout.setOnClickListener(cellOcl);
568         cellLayout.setOnHoverListener(cellOhl);
569         cellLayout.setOnLongClickListener(null);
570         cellLayout.setOnGenericMotionListener(null);
571         cellLayout.setOnInterceptedLongPressListener(null);
572
573         SharedPreferences pref = U.getSharedPreferences(context);
574         SharedPreferences.Editor editor = pref.edit();
575         editor.remove("dashboard_widget_" + cellId);
576
577         if(tempRemove) {
578             editor.putBoolean("dashboard_widget_" + cellId + "_placeholder", true);
579             addPlaceholder(cellId);
580         } else
581             editor.remove("dashboard_widget_" + cellId + "_provider");
582
583         editor.apply();
584     }
585
586     private void addPlaceholder(int cellId) {
587         FrameLayout placeholder = cells.get(cellId).findViewById(R.id.placeholder);
588         SharedPreferences pref = U.getSharedPreferences(context);
589         String providerName = pref.getString("dashboard_widget_" + cellId + "_provider", "null");
590
591         if(!providerName.equals("null")) {
592             ImageView imageView = placeholder.findViewById(R.id.placeholder_image);
593             ComponentName componentName = ComponentName.unflattenFromString(providerName);
594
595             List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
596             for(AppWidgetProviderInfo info : providerInfoList) {
597                 if(info.provider.equals(componentName)) {
598                     Drawable drawable = info.loadPreviewImage(context, -1);
599                     if(drawable == null) drawable = info.loadIcon(context, -1);
600
601                     ColorMatrix matrix = new ColorMatrix();
602                     matrix.setSaturation(0);
603
604                     imageView.setImageDrawable(drawable);
605                     imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
606                     break;
607                 }
608             }
609         }
610
611         placeholder.setVisibility(View.VISIBLE);
612     }
613 }