OSDN Git Service

6dd18938c4869e9232a06acb16ab3d254f86a8e5
[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.SharedPreferences;
34 import android.content.pm.PackageManager;
35 import android.content.res.Configuration;
36 import android.graphics.Color;
37 import android.graphics.ColorMatrix;
38 import android.graphics.ColorMatrixColorFilter;
39 import android.graphics.drawable.Drawable;
40 import android.os.Build;
41 import android.os.Bundle;
42 import android.os.Handler;
43 import android.os.Process;
44
45 import androidx.annotation.NonNull;
46 import androidx.annotation.VisibleForTesting;
47 import androidx.core.graphics.ColorUtils;
48 import android.util.SparseArray;
49 import android.view.MotionEvent;
50 import android.view.PointerIcon;
51 import android.view.View;
52 import android.view.ViewGroup;
53 import android.view.WindowManager;
54 import android.widget.FrameLayout;
55 import android.widget.ImageView;
56 import android.widget.LinearLayout;
57 import android.widget.TextView;
58 import android.widget.Toast;
59
60 import com.farmerbb.taskbar.R;
61 import com.farmerbb.taskbar.activity.DashboardActivity;
62 import com.farmerbb.taskbar.helper.LauncherHelper;
63 import com.farmerbb.taskbar.util.Constants;
64 import com.farmerbb.taskbar.util.TaskbarPosition;
65 import com.farmerbb.taskbar.helper.DashboardHelper;
66 import com.farmerbb.taskbar.widget.DashboardCell;
67 import com.farmerbb.taskbar.helper.FreeformHackHelper;
68 import com.farmerbb.taskbar.util.U;
69
70 import java.util.List;
71
72 import static com.farmerbb.taskbar.util.Constants.*;
73
74 public class DashboardController extends UIController {
75
76     private AppWidgetManager mAppWidgetManager;
77     private AppWidgetHost mAppWidgetHost;
78
79     private LinearLayout layout;
80
81     private SparseArray<DashboardCell> cells = new SparseArray<>();
82     private SparseArray<AppWidgetHostView> widgets = new SparseArray<>();
83
84     private final int APPWIDGET_HOST_ID = 123;
85
86     private int columns;
87     private int rows;
88     private int maxSize;
89     private int previouslySelectedCell = -1;
90
91     private View.OnClickListener ocl = view -> toggleDashboard();
92
93     private View.OnClickListener cellOcl = view -> cellClick(view, true);
94
95     private View.OnHoverListener cellOhl = (v, event) -> {
96         cellClick(v, false);
97
98         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
99             v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
100
101         return false;
102     };
103
104     private View.OnLongClickListener olcl = v -> {
105         cellLongClick(v);
106         return true;
107     };
108
109     private View.OnGenericMotionListener ogml = (view, motionEvent) -> {
110         if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
111                 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
112             cellLongClick(view);
113
114         return false;
115     };
116
117     private DashboardCell.OnInterceptedLongPressListener listener = this::cellLongClick;
118
119     private BroadcastReceiver toggleReceiver = new BroadcastReceiver() {
120         @Override
121         public void onReceive(Context context, Intent intent) {
122             toggleDashboard();
123         }
124     };
125
126     private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
127         @Override
128         public void onReceive(Context context, Intent intent) {
129             fadeIn();
130             addWidget(intent);
131         }
132     };
133
134     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
135         @Override
136         public void onReceive(Context context, Intent intent) {
137             fadeIn();
138
139             if(intent.hasExtra(EXTRA_CELL_ID)) {
140                 int cellId = intent.getExtras().getInt(EXTRA_CELL_ID, -1);
141
142                 removeWidget(cellId, false);
143             }
144         }
145     };
146
147     private BroadcastReceiver hideReceiver = new BroadcastReceiver() {
148         @Override
149         public void onReceive(Context context, Intent intent) {
150             hideDashboard();
151         }
152     };
153
154     public DashboardController(Context context) {
155         super(context);
156     }
157
158     @TargetApi(Build.VERSION_CODES.M)
159     @Override
160     public void onCreateHost(UIHost host) {
161         if(U.getBooleanPrefWithDefault(context, PREF_DASHBOARD))
162             init(context, host, () -> drawDashboard(host));
163         else
164             host.terminate();
165     }
166
167     @SuppressLint("RtlHardcoded")
168     private void drawDashboard(UIHost host) {
169         final ViewParams params = new ViewParams(
170                 WindowManager.LayoutParams.MATCH_PARENT,
171                 WindowManager.LayoutParams.MATCH_PARENT,
172                 -1,
173                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
174                 getBottomMargin(context, host)
175         );
176
177         // Initialize views
178         layout = new LinearLayout(context);
179         layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
180         layout.setVisibility(View.GONE);
181         layout.setAlpha(0);
182
183         SharedPreferences pref = U.getSharedPreferences(context);
184         int width = U.getIntPrefWithDefault(context, PREF_DASHBOARD_WIDTH);
185         int height = U.getIntPrefWithDefault(context, PREF_DASHBOARD_HEIGHT);
186
187         int orientation = U.getDisplayOrientation(context);
188         boolean isPortrait = orientation == Configuration.ORIENTATION_PORTRAIT;
189         boolean isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE;
190
191         if(isPortrait) {
192             columns = height;
193             rows = width;
194         }
195
196         if(isLandscape) {
197             columns = width;
198             rows = height;
199         }
200
201         maxSize = columns * rows;
202
203         int backgroundTint = U.getBackgroundTint(context);
204         int accentColor = U.getAccentColor(context);
205         int accentColorAlt = accentColor;
206         accentColorAlt = ColorUtils.setAlphaComponent(accentColorAlt, Color.alpha(accentColorAlt) / 3);
207
208         int cellCount = 0;
209
210         for(int i = 0; i < columns; i++) {
211             LinearLayout layout2 = new LinearLayout(context);
212             layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
213             layout2.setOrientation(LinearLayout.VERTICAL);
214
215             for(int j = 0; j < rows; j++) {
216                 DashboardCell cellLayout = (DashboardCell) View.inflate(context, R.layout.tb_dashboard, null);
217                 cellLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
218                 cellLayout.setBackgroundColor(backgroundTint);
219                 cellLayout.setOnClickListener(cellOcl);
220                 cellLayout.setOnHoverListener(cellOhl);
221                 cellLayout.setFocusable(false);
222
223                 TextView empty = cellLayout.findViewById(R.id.empty);
224                 empty.setBackgroundColor(accentColorAlt);
225                 empty.setTextColor(accentColor);
226
227                 Bundle bundle = new Bundle();
228                 bundle.putInt(EXTRA_CELL_ID, cellCount);
229
230                 cellLayout.setTag(bundle);
231                 cells.put(cellCount, cellLayout);
232                 cellCount++;
233
234                 layout2.addView(cellLayout);
235             }
236
237             layout.addView(layout2);
238         }
239
240         mAppWidgetManager = AppWidgetManager.getInstance(context);
241         mAppWidgetHost = new AppWidgetHost(context, APPWIDGET_HOST_ID);
242         mAppWidgetHost.startListening();
243
244         for(int i = 0; i < maxSize; i++) {
245             int appWidgetId = pref.getInt(PREF_DASHBOARD_WIDGET_PREFIX + i, -1);
246             if(appWidgetId != -1)
247                 addWidget(appWidgetId, i, false);
248             else if (pref.getBoolean(
249                     PREF_DASHBOARD_WIDGET_PREFIX
250                             + i
251                             + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
252                     false)) {
253                 addPlaceholder(i);
254             }
255         }
256
257         mAppWidgetHost.stopListening();
258
259         U.registerReceiver(context, toggleReceiver, ACTION_TOGGLE_DASHBOARD);
260         U.registerReceiver(context, addWidgetReceiver, ACTION_ADD_WIDGET_COMPLETED);
261         U.registerReceiver(context, removeWidgetReceiver, ACTION_REMOVE_WIDGET_COMPLETED);
262         U.registerReceiver(context, hideReceiver, ACTION_HIDE_DASHBOARD);
263
264         host.addView(layout, params);
265
266         new Handler().postDelayed(() ->
267                 updatePaddingSize(
268                         context,
269                         layout,
270                         TaskbarPosition.getTaskbarPosition(context)
271                 ),
272                 100
273         );
274     }
275
276     @VisibleForTesting
277     void updatePaddingSize(Context context, LinearLayout layout, String position) {
278         int paddingSize = context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size);
279
280         switch(position) {
281             case POSITION_TOP_VERTICAL_LEFT:
282             case POSITION_BOTTOM_VERTICAL_LEFT:
283                 layout.setPadding(paddingSize, 0, 0, 0);
284                 break;
285             case POSITION_TOP_LEFT:
286             case POSITION_TOP_RIGHT:
287                 layout.setPadding(0, paddingSize, 0, 0);
288                 break;
289             case POSITION_TOP_VERTICAL_RIGHT:
290             case POSITION_BOTTOM_VERTICAL_RIGHT:
291                 layout.setPadding(0, 0, paddingSize, 0);
292                 break;
293             case POSITION_BOTTOM_LEFT:
294             case POSITION_BOTTOM_RIGHT:
295                 layout.setPadding(0, 0, 0, paddingSize);
296                 break;
297         }
298     }
299
300     private void toggleDashboard() {
301         if(layout.getVisibility() == View.GONE)
302             showDashboard();
303         else
304             hideDashboard();
305     }
306
307     @TargetApi(Build.VERSION_CODES.N)
308     private void showDashboard() {
309         if(layout.getVisibility() == View.GONE) {
310             layout.setOnClickListener(ocl);
311             fadeIn();
312
313             U.sendBroadcast(context, ACTION_DASHBOARD_APPEARING);
314             U.sendBroadcast(context, ACTION_HIDE_START_MENU);
315
316             boolean inFreeformMode = FreeformHackHelper.getInstance().isInFreeformWorkspace();
317
318             Intent intent = U.getThemedIntent(context, DashboardActivity.class);
319             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
320             intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
321
322             if(inFreeformMode) {
323                 if(U.hasBrokenSetLaunchBoundsApi())
324                     intent.putExtra("context_menu_fix", true);
325
326                 U.startActivityMaximized(context, intent);
327             } else
328                 context.startActivity(intent);
329
330             for(int i = 0; i < maxSize; i++) {
331                 final DashboardCell cellLayout = cells.get(i);
332                 final AppWidgetHostView hostView = widgets.get(i);
333
334                 if(hostView != null) {
335                     try {
336                         context.getPackageManager().getApplicationInfo(hostView.getAppWidgetInfo().provider.getPackageName(), 0);
337                         hostView.post(() -> {
338                             ViewGroup.LayoutParams params = hostView.getLayoutParams();
339                             params.width = cellLayout.getWidth();
340                             params.height = cellLayout.getHeight();
341                             hostView.setLayoutParams(params);
342                             hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
343                         });
344                     } catch (PackageManager.NameNotFoundException e) {
345                         removeWidget(i, false);
346                     } catch (NullPointerException e) {
347                         removeWidget(i, true);
348                     }
349                 }
350             }
351
352             final SharedPreferences pref = U.getSharedPreferences(context);
353             if(!pref.getBoolean(PREF_DASHBOARD_TUTORIAL_SHOWN, false)) {
354                 U.showToastLong(context, R.string.tb_dashboard_tutorial);
355                 pref.edit().putBoolean(PREF_DASHBOARD_TUTORIAL_SHOWN, true).apply();
356             }
357         }
358     }
359
360     private void hideDashboard() {
361         if(layout.getVisibility() == View.VISIBLE) {
362             layout.setOnClickListener(null);
363             fadeOut(true);
364
365             for(int i = 0; i < maxSize; i++) {
366                 FrameLayout frameLayout = cells.get(i);
367                 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
368             }
369
370             previouslySelectedCell = -1;
371         }
372     }
373
374     private void fadeIn() {
375         mAppWidgetHost.startListening();
376
377         DashboardHelper.getInstance().setDashboardOpen(true);
378
379         layout.setVisibility(View.VISIBLE);
380         layout.animate()
381                 .alpha(1)
382                 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
383                 .setListener(null);
384     }
385
386     private void fadeOut(final boolean sendIntent) {
387         mAppWidgetHost.stopListening();
388
389         DashboardHelper.getInstance().setDashboardOpen(false);
390
391         layout.animate()
392                 .alpha(0)
393                 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
394                 .setListener(new AnimatorListenerAdapter() {
395                     @Override
396                     public void onAnimationEnd(Animator animation) {
397                         layout.setVisibility(View.GONE);
398                         if(sendIntent) {
399                             U.sendBroadcast(context, ACTION_DASHBOARD_DISAPPEARING);
400                         }
401                     }
402                 });
403     }
404
405     @TargetApi(Build.VERSION_CODES.M)
406     @Override
407     public void onRecreateHost(UIHost host) {
408         if(layout != null) {
409             try {
410                 host.removeView(layout);
411             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
412
413             SharedPreferences pref = U.getSharedPreferences(context);
414             if(U.canDrawOverlays(context))
415                 drawDashboard(host);
416             else {
417                 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
418
419                 host.terminate();
420             }
421         }
422     }
423
424     @Override
425     public void onDestroyHost(UIHost host) {
426         if(layout != null)
427             try {
428                 host.removeView(layout);
429             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
430
431         U.unregisterReceiver(context, toggleReceiver);
432         U.unregisterReceiver(context, addWidgetReceiver);
433         U.unregisterReceiver(context, removeWidgetReceiver);
434         U.unregisterReceiver(context, hideReceiver);
435
436         SharedPreferences pref = U.getSharedPreferences(context);
437         if(!LauncherHelper.getInstance().isOnSecondaryHomeScreen(context)
438                 || !pref.getBoolean(PREF_DONT_STOP_DASHBOARD, false))
439             U.sendBroadcast(context, ACTION_DASHBOARD_DISAPPEARING);
440
441         pref.edit().remove(PREF_DONT_STOP_DASHBOARD).apply();
442     }
443
444     private void cellClick(View view, boolean isActualClick) {
445         Bundle bundle = (Bundle) view.getTag();
446         int cellId = bundle.getInt(EXTRA_CELL_ID);
447         int appWidgetId = bundle.getInt(EXTRA_APPWIDGET_ID, -1);
448
449         int currentlySelectedCell = appWidgetId == -1 ? cellId : -1;
450
451         SharedPreferences pref = U.getSharedPreferences(context);
452         boolean shouldShowPlaceholder =
453                 pref.getBoolean(
454                         PREF_DASHBOARD_WIDGET_PREFIX
455                                 + cellId
456                                 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
457                         false
458                 );
459         if(isActualClick && ((appWidgetId == -1 && currentlySelectedCell == previouslySelectedCell) || shouldShowPlaceholder)) {
460             fadeOut(false);
461
462             FrameLayout frameLayout = cells.get(currentlySelectedCell);
463             frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
464
465             Intent intent = new Intent(ACTION_ADD_WIDGET_REQUESTED);
466             intent.putExtra(EXTRA_APPWIDGET_ID, APPWIDGET_HOST_ID);
467             intent.putExtra(EXTRA_CELL_ID, cellId);
468             U.sendBroadcast(context, intent);
469
470             if(shouldShowPlaceholder) {
471                 String providerName =
472                         pref.getString(
473                                 PREF_DASHBOARD_WIDGET_PREFIX
474                                         + cellId
475                                         + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
476                                 "null"
477                         );
478                 if(!providerName.equals("null")) {
479                     ComponentName componentName = ComponentName.unflattenFromString(providerName);
480
481                     List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
482                     for(AppWidgetProviderInfo info : providerInfoList) {
483                         if(info.provider.equals(componentName)) {
484                             U.showToast(context, context.getString(R.string.tb_widget_restore_toast, info.loadLabel(context.getPackageManager())), Toast.LENGTH_SHORT);
485                             break;
486                         }
487                     }
488                 }
489             }
490
491             previouslySelectedCell = -1;
492         } else {
493             for(int i = 0; i < maxSize; i++) {
494                 FrameLayout frameLayout = cells.get(i);
495                 frameLayout.findViewById(R.id.empty).setVisibility(i == currentlySelectedCell && !shouldShowPlaceholder ? View.VISIBLE : View.GONE);
496             }
497
498             previouslySelectedCell = currentlySelectedCell;
499         }
500     }
501
502     private void cellLongClick(View view) {
503         fadeOut(false);
504
505         Bundle bundle = (Bundle) view.getTag();
506         int cellId = bundle.getInt(EXTRA_CELL_ID);
507
508         Intent intent = new Intent(ACTION_REMOVE_WIDGET_REQUESTED);
509         intent.putExtra(EXTRA_CELL_ID, cellId);
510         U.sendBroadcast(context, intent);
511     }
512
513     @VisibleForTesting
514     void addWidget(Intent intent) {
515         if (intent.hasExtra(EXTRA_APPWIDGET_ID) && intent.hasExtra(EXTRA_CELL_ID)) {
516             int appWidgetId = intent.getExtras().getInt(EXTRA_APPWIDGET_ID, -1);
517             int cellId = intent.getExtras().getInt(EXTRA_CELL_ID, -1);
518
519             addWidget(appWidgetId, cellId, true);
520         }
521     }
522
523     private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
524         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
525
526         final DashboardCell cellLayout = cells.get(cellId);
527         final AppWidgetHostView hostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
528         hostView.setAppWidget(appWidgetId, appWidgetInfo);
529
530         Bundle bundle = new Bundle();
531         bundle.putInt(EXTRA_CELL_ID, cellId);
532         hostView.setTag(bundle);
533
534         cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
535         cellLayout.findViewById(R.id.placeholder).setVisibility(View.GONE);
536         cellLayout.setOnLongClickListener(olcl);
537         cellLayout.setOnGenericMotionListener(ogml);
538         cellLayout.setOnInterceptedLongPressListener(listener);
539
540         LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
541         linearLayout.addView(hostView);
542
543         Bundle bundle2 = (Bundle) cellLayout.getTag();
544         bundle2.putInt(EXTRA_APPWIDGET_ID, appWidgetId);
545         cellLayout.setTag(bundle2);
546
547         widgets.put(cellId, hostView);
548
549         if (shouldSave) {
550             saveWidgetInfo(context, appWidgetInfo, cellId, appWidgetId);
551         }
552
553         new Handler().post(() -> {
554             ViewGroup.LayoutParams params = hostView.getLayoutParams();
555             params.width = cellLayout.getWidth();
556             params.height = cellLayout.getHeight();
557             hostView.setLayoutParams(params);
558             hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
559         });
560     }
561
562     @VisibleForTesting
563     void saveWidgetInfo(Context context,
564                         AppWidgetProviderInfo appWidgetInfo,
565                         int cellId,
566                         int appWidgetId) {
567         SharedPreferences pref = U.getSharedPreferences(context);
568         SharedPreferences.Editor editor = pref.edit();
569         editor.putInt(PREF_DASHBOARD_WIDGET_PREFIX + cellId, appWidgetId);
570         editor.putString(
571                 PREF_DASHBOARD_WIDGET_PREFIX + cellId + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
572                 appWidgetInfo.provider.flattenToString()
573         );
574         editor.remove(
575                 PREF_DASHBOARD_WIDGET_PREFIX
576                         + cellId
577                         + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX
578         );
579         editor.apply();
580     }
581
582     private void removeWidget(int cellId, boolean tempRemove) {
583         widgets.remove(cellId);
584
585         DashboardCell cellLayout = cells.get(cellId);
586         Bundle bundle = (Bundle) cellLayout.getTag();
587
588         mAppWidgetHost.deleteAppWidgetId(bundle.getInt(EXTRA_APPWIDGET_ID));
589         bundle.remove(EXTRA_APPWIDGET_ID);
590
591         LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
592         linearLayout.removeAllViews();
593
594         cellLayout.setTag(bundle);
595         cellLayout.setOnClickListener(cellOcl);
596         cellLayout.setOnHoverListener(cellOhl);
597         cellLayout.setOnLongClickListener(null);
598         cellLayout.setOnGenericMotionListener(null);
599         cellLayout.setOnInterceptedLongPressListener(null);
600
601         SharedPreferences pref = U.getSharedPreferences(context);
602         SharedPreferences.Editor editor = pref.edit();
603         editor.remove(PREF_DASHBOARD_WIDGET_PREFIX + cellId);
604
605         if (tempRemove) {
606             editor.putBoolean(
607                     PREF_DASHBOARD_WIDGET_PREFIX
608                             + cellId
609                             + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
610                     true
611             );
612             addPlaceholder(cellId);
613         } else {
614             editor.remove(
615                     PREF_DASHBOARD_WIDGET_PREFIX
616                             + cellId
617                             + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX
618             );
619         }
620         editor.apply();
621     }
622
623     private void addPlaceholder(int cellId) {
624         FrameLayout placeholder = cells.get(cellId).findViewById(R.id.placeholder);
625         SharedPreferences pref = U.getSharedPreferences(context);
626         String providerName =
627                 pref.getString(
628                         PREF_DASHBOARD_WIDGET_PREFIX
629                                 + cellId
630                                 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
631                         "null"
632                 );
633
634         if(!providerName.equals("null")) {
635             ImageView imageView = placeholder.findViewById(R.id.placeholder_image);
636             ComponentName componentName = ComponentName.unflattenFromString(providerName);
637
638             List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
639             for(AppWidgetProviderInfo info : providerInfoList) {
640                 if(info.provider.equals(componentName)) {
641                     Drawable drawable = info.loadPreviewImage(context, -1);
642                     if(drawable == null) drawable = info.loadIcon(context, -1);
643
644                     ColorMatrix matrix = new ColorMatrix();
645                     matrix.setSaturation(0);
646
647                     imageView.setImageDrawable(drawable);
648                     imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
649                     break;
650                 }
651             }
652         }
653
654         placeholder.setVisibility(View.VISIBLE);
655     }
656 }