1 /* Based on code by Leonardo Fischer
2 * See https://github.com/lgfischer/WidgetHostExample
4 * Copyright 2016 Braden Farmer
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package com.farmerbb.taskbar.ui;
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;
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;
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;
70 import java.util.List;
72 import static com.farmerbb.taskbar.util.Constants.*;
74 public class DashboardController extends UIController {
76 private AppWidgetManager mAppWidgetManager;
77 private AppWidgetHost mAppWidgetHost;
79 private LinearLayout layout;
81 private SparseArray<DashboardCell> cells = new SparseArray<>();
82 private SparseArray<AppWidgetHostView> widgets = new SparseArray<>();
84 private final int APPWIDGET_HOST_ID = 123;
89 private int previouslySelectedCell = -1;
91 private View.OnClickListener ocl = view -> toggleDashboard();
93 private View.OnClickListener cellOcl = view -> cellClick(view, true);
95 private View.OnHoverListener cellOhl = (v, event) -> {
98 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
99 v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
104 private View.OnLongClickListener olcl = v -> {
109 private View.OnGenericMotionListener ogml = (view, motionEvent) -> {
110 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
111 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
117 private DashboardCell.OnInterceptedLongPressListener listener = this::cellLongClick;
119 private BroadcastReceiver toggleReceiver = new BroadcastReceiver() {
121 public void onReceive(Context context, Intent intent) {
126 private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
128 public void onReceive(Context context, Intent intent) {
134 private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
136 public void onReceive(Context context, Intent intent) {
139 if(intent.hasExtra(EXTRA_CELL_ID)) {
140 int cellId = intent.getExtras().getInt(EXTRA_CELL_ID, -1);
142 removeWidget(cellId, false);
147 private BroadcastReceiver hideReceiver = new BroadcastReceiver() {
149 public void onReceive(Context context, Intent intent) {
154 public DashboardController(Context context) {
158 @TargetApi(Build.VERSION_CODES.M)
160 public void onCreateHost(UIHost host) {
161 if(U.getBooleanPrefWithDefault(context, PREF_DASHBOARD))
162 init(context, host, () -> drawDashboard(host));
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,
173 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
174 getBottomMargin(context, host)
178 layout = new LinearLayout(context);
179 layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
180 layout.setVisibility(View.GONE);
183 SharedPreferences pref = U.getSharedPreferences(context);
184 int width = U.getIntPrefWithDefault(context, PREF_DASHBOARD_WIDTH);
185 int height = U.getIntPrefWithDefault(context, PREF_DASHBOARD_HEIGHT);
187 int orientation = U.getDisplayOrientation(context);
188 boolean isPortrait = orientation == Configuration.ORIENTATION_PORTRAIT;
189 boolean isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE;
201 maxSize = columns * rows;
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);
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);
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);
223 TextView empty = cellLayout.findViewById(R.id.empty);
224 empty.setBackgroundColor(accentColorAlt);
225 empty.setTextColor(accentColor);
227 Bundle bundle = new Bundle();
228 bundle.putInt(EXTRA_CELL_ID, cellCount);
230 cellLayout.setTag(bundle);
231 cells.put(cellCount, cellLayout);
234 layout2.addView(cellLayout);
237 layout.addView(layout2);
240 mAppWidgetManager = AppWidgetManager.getInstance(context);
241 mAppWidgetHost = new AppWidgetHost(context, APPWIDGET_HOST_ID);
242 mAppWidgetHost.startListening();
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
251 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
257 mAppWidgetHost.stopListening();
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);
264 host.addView(layout, params);
266 new Handler().postDelayed(() ->
270 TaskbarPosition.getTaskbarPosition(context)
277 void updatePaddingSize(Context context, LinearLayout layout, String position) {
278 int paddingSize = context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size);
281 case POSITION_TOP_VERTICAL_LEFT:
282 case POSITION_BOTTOM_VERTICAL_LEFT:
283 layout.setPadding(paddingSize, 0, 0, 0);
285 case POSITION_TOP_LEFT:
286 case POSITION_TOP_RIGHT:
287 layout.setPadding(0, paddingSize, 0, 0);
289 case POSITION_TOP_VERTICAL_RIGHT:
290 case POSITION_BOTTOM_VERTICAL_RIGHT:
291 layout.setPadding(0, 0, paddingSize, 0);
293 case POSITION_BOTTOM_LEFT:
294 case POSITION_BOTTOM_RIGHT:
295 layout.setPadding(0, 0, 0, paddingSize);
300 private void toggleDashboard() {
301 if(layout.getVisibility() == View.GONE)
307 @TargetApi(Build.VERSION_CODES.N)
308 private void showDashboard() {
309 if(layout.getVisibility() == View.GONE) {
310 layout.setOnClickListener(ocl);
313 U.sendBroadcast(context, ACTION_DASHBOARD_APPEARING);
314 U.sendBroadcast(context, ACTION_HIDE_START_MENU);
316 boolean inFreeformMode = FreeformHackHelper.getInstance().isInFreeformWorkspace();
318 Intent intent = U.getThemedIntent(context, DashboardActivity.class);
319 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
320 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
322 if (inFreeformMode) {
323 if (U.hasBrokenSetLaunchBoundsApi()) {
324 intent.putExtra(EXTRA_CONTEXT_MENU_FIX, true);
327 U.startActivityMaximized(context, intent);
329 context.startActivity(intent);
332 for(int i = 0; i < maxSize; i++) {
333 final DashboardCell cellLayout = cells.get(i);
334 final AppWidgetHostView hostView = widgets.get(i);
336 if(hostView != null) {
338 context.getPackageManager().getApplicationInfo(hostView.getAppWidgetInfo().provider.getPackageName(), 0);
339 hostView.post(() -> {
340 ViewGroup.LayoutParams params = hostView.getLayoutParams();
341 params.width = cellLayout.getWidth();
342 params.height = cellLayout.getHeight();
343 hostView.setLayoutParams(params);
344 hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
346 } catch (PackageManager.NameNotFoundException e) {
347 removeWidget(i, false);
348 } catch (NullPointerException e) {
349 removeWidget(i, true);
354 final SharedPreferences pref = U.getSharedPreferences(context);
355 if(!pref.getBoolean(PREF_DASHBOARD_TUTORIAL_SHOWN, false)) {
356 U.showToastLong(context, R.string.tb_dashboard_tutorial);
357 pref.edit().putBoolean(PREF_DASHBOARD_TUTORIAL_SHOWN, true).apply();
362 private void hideDashboard() {
363 if(layout.getVisibility() == View.VISIBLE) {
364 layout.setOnClickListener(null);
367 for(int i = 0; i < maxSize; i++) {
368 FrameLayout frameLayout = cells.get(i);
369 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
372 previouslySelectedCell = -1;
376 private void fadeIn() {
377 mAppWidgetHost.startListening();
379 DashboardHelper.getInstance().setDashboardOpen(true);
381 layout.setVisibility(View.VISIBLE);
384 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
388 private void fadeOut(final boolean sendIntent) {
389 mAppWidgetHost.stopListening();
391 DashboardHelper.getInstance().setDashboardOpen(false);
395 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
396 .setListener(new AnimatorListenerAdapter() {
398 public void onAnimationEnd(Animator animation) {
399 layout.setVisibility(View.GONE);
401 U.sendBroadcast(context, ACTION_DASHBOARD_DISAPPEARING);
407 @TargetApi(Build.VERSION_CODES.M)
409 public void onRecreateHost(UIHost host) {
412 host.removeView(layout);
413 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
415 SharedPreferences pref = U.getSharedPreferences(context);
416 if(U.canDrawOverlays(context))
419 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
427 public void onDestroyHost(UIHost host) {
430 host.removeView(layout);
431 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
433 U.unregisterReceiver(context, toggleReceiver);
434 U.unregisterReceiver(context, addWidgetReceiver);
435 U.unregisterReceiver(context, removeWidgetReceiver);
436 U.unregisterReceiver(context, hideReceiver);
438 SharedPreferences pref = U.getSharedPreferences(context);
439 if(!LauncherHelper.getInstance().isOnSecondaryHomeScreen(context)
440 || !pref.getBoolean(PREF_DONT_STOP_DASHBOARD, false))
441 U.sendBroadcast(context, ACTION_DASHBOARD_DISAPPEARING);
443 pref.edit().remove(PREF_DONT_STOP_DASHBOARD).apply();
446 private void cellClick(View view, boolean isActualClick) {
447 Bundle bundle = (Bundle) view.getTag();
448 int cellId = bundle.getInt(EXTRA_CELL_ID);
449 int appWidgetId = bundle.getInt(EXTRA_APPWIDGET_ID, -1);
451 int currentlySelectedCell = appWidgetId == -1 ? cellId : -1;
453 SharedPreferences pref = U.getSharedPreferences(context);
454 boolean shouldShowPlaceholder =
456 PREF_DASHBOARD_WIDGET_PREFIX
458 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
461 if(isActualClick && ((appWidgetId == -1 && currentlySelectedCell == previouslySelectedCell) || shouldShowPlaceholder)) {
464 FrameLayout frameLayout = cells.get(currentlySelectedCell);
465 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
467 Intent intent = new Intent(ACTION_ADD_WIDGET_REQUESTED);
468 intent.putExtra(EXTRA_APPWIDGET_ID, APPWIDGET_HOST_ID);
469 intent.putExtra(EXTRA_CELL_ID, cellId);
470 U.sendBroadcast(context, intent);
472 if(shouldShowPlaceholder) {
473 String providerName =
475 PREF_DASHBOARD_WIDGET_PREFIX
477 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
480 if(!providerName.equals("null")) {
481 ComponentName componentName = ComponentName.unflattenFromString(providerName);
483 List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
484 for(AppWidgetProviderInfo info : providerInfoList) {
485 if(info.provider.equals(componentName)) {
486 U.showToast(context, context.getString(R.string.tb_widget_restore_toast, info.loadLabel(context.getPackageManager())), Toast.LENGTH_SHORT);
493 previouslySelectedCell = -1;
495 for(int i = 0; i < maxSize; i++) {
496 FrameLayout frameLayout = cells.get(i);
497 frameLayout.findViewById(R.id.empty).setVisibility(i == currentlySelectedCell && !shouldShowPlaceholder ? View.VISIBLE : View.GONE);
500 previouslySelectedCell = currentlySelectedCell;
504 private void cellLongClick(View view) {
507 Bundle bundle = (Bundle) view.getTag();
508 int cellId = bundle.getInt(EXTRA_CELL_ID);
510 Intent intent = new Intent(ACTION_REMOVE_WIDGET_REQUESTED);
511 intent.putExtra(EXTRA_CELL_ID, cellId);
512 U.sendBroadcast(context, intent);
516 void addWidget(Intent intent) {
517 if (intent.hasExtra(EXTRA_APPWIDGET_ID) && intent.hasExtra(EXTRA_CELL_ID)) {
518 int appWidgetId = intent.getExtras().getInt(EXTRA_APPWIDGET_ID, -1);
519 int cellId = intent.getExtras().getInt(EXTRA_CELL_ID, -1);
521 addWidget(appWidgetId, cellId, true);
525 private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
526 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
528 final DashboardCell cellLayout = cells.get(cellId);
529 final AppWidgetHostView hostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
530 hostView.setAppWidget(appWidgetId, appWidgetInfo);
532 Bundle bundle = new Bundle();
533 bundle.putInt(EXTRA_CELL_ID, cellId);
534 hostView.setTag(bundle);
536 cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
537 cellLayout.findViewById(R.id.placeholder).setVisibility(View.GONE);
538 cellLayout.setOnLongClickListener(olcl);
539 cellLayout.setOnGenericMotionListener(ogml);
540 cellLayout.setOnInterceptedLongPressListener(listener);
542 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
543 linearLayout.addView(hostView);
545 Bundle bundle2 = (Bundle) cellLayout.getTag();
546 bundle2.putInt(EXTRA_APPWIDGET_ID, appWidgetId);
547 cellLayout.setTag(bundle2);
549 widgets.put(cellId, hostView);
552 saveWidgetInfo(context, appWidgetInfo, cellId, appWidgetId);
555 new Handler().post(() -> {
556 ViewGroup.LayoutParams params = hostView.getLayoutParams();
557 params.width = cellLayout.getWidth();
558 params.height = cellLayout.getHeight();
559 hostView.setLayoutParams(params);
560 hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
565 void saveWidgetInfo(Context context,
566 AppWidgetProviderInfo appWidgetInfo,
569 SharedPreferences pref = U.getSharedPreferences(context);
570 SharedPreferences.Editor editor = pref.edit();
571 editor.putInt(PREF_DASHBOARD_WIDGET_PREFIX + cellId, appWidgetId);
573 PREF_DASHBOARD_WIDGET_PREFIX + cellId + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
574 appWidgetInfo.provider.flattenToString()
577 PREF_DASHBOARD_WIDGET_PREFIX
579 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX
584 private void removeWidget(int cellId, boolean tempRemove) {
585 widgets.remove(cellId);
587 DashboardCell cellLayout = cells.get(cellId);
588 Bundle bundle = (Bundle) cellLayout.getTag();
590 mAppWidgetHost.deleteAppWidgetId(bundle.getInt(EXTRA_APPWIDGET_ID));
591 bundle.remove(EXTRA_APPWIDGET_ID);
593 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
594 linearLayout.removeAllViews();
596 cellLayout.setTag(bundle);
597 cellLayout.setOnClickListener(cellOcl);
598 cellLayout.setOnHoverListener(cellOhl);
599 cellLayout.setOnLongClickListener(null);
600 cellLayout.setOnGenericMotionListener(null);
601 cellLayout.setOnInterceptedLongPressListener(null);
603 SharedPreferences pref = U.getSharedPreferences(context);
604 SharedPreferences.Editor editor = pref.edit();
605 editor.remove(PREF_DASHBOARD_WIDGET_PREFIX + cellId);
609 PREF_DASHBOARD_WIDGET_PREFIX
611 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
614 addPlaceholder(cellId);
617 PREF_DASHBOARD_WIDGET_PREFIX
619 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX
625 private void addPlaceholder(int cellId) {
626 FrameLayout placeholder = cells.get(cellId).findViewById(R.id.placeholder);
627 SharedPreferences pref = U.getSharedPreferences(context);
628 String providerName =
630 PREF_DASHBOARD_WIDGET_PREFIX
632 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
636 if(!providerName.equals("null")) {
637 ImageView imageView = placeholder.findViewById(R.id.placeholder_image);
638 ComponentName componentName = ComponentName.unflattenFromString(providerName);
640 List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
641 for(AppWidgetProviderInfo info : providerInfoList) {
642 if(info.provider.equals(componentName)) {
643 Drawable drawable = info.loadPreviewImage(context, -1);
644 if(drawable == null) drawable = info.loadIcon(context, -1);
646 ColorMatrix matrix = new ColorMatrix();
647 matrix.setSaturation(0);
649 imageView.setImageDrawable(drawable);
650 imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
656 placeholder.setVisibility(View.VISIBLE);