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);
323 if(U.hasBrokenSetLaunchBoundsApi())
324 intent.putExtra("context_menu_fix", true);
326 U.startActivityMaximized(context, intent);
328 context.startActivity(intent);
330 for(int i = 0; i < maxSize; i++) {
331 final DashboardCell cellLayout = cells.get(i);
332 final AppWidgetHostView hostView = widgets.get(i);
334 if(hostView != null) {
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());
344 } catch (PackageManager.NameNotFoundException e) {
345 removeWidget(i, false);
346 } catch (NullPointerException e) {
347 removeWidget(i, true);
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();
360 private void hideDashboard() {
361 if(layout.getVisibility() == View.VISIBLE) {
362 layout.setOnClickListener(null);
365 for(int i = 0; i < maxSize; i++) {
366 FrameLayout frameLayout = cells.get(i);
367 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
370 previouslySelectedCell = -1;
374 private void fadeIn() {
375 mAppWidgetHost.startListening();
377 DashboardHelper.getInstance().setDashboardOpen(true);
379 layout.setVisibility(View.VISIBLE);
382 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
386 private void fadeOut(final boolean sendIntent) {
387 mAppWidgetHost.stopListening();
389 DashboardHelper.getInstance().setDashboardOpen(false);
393 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
394 .setListener(new AnimatorListenerAdapter() {
396 public void onAnimationEnd(Animator animation) {
397 layout.setVisibility(View.GONE);
399 U.sendBroadcast(context, ACTION_DASHBOARD_DISAPPEARING);
405 @TargetApi(Build.VERSION_CODES.M)
407 public void onRecreateHost(UIHost host) {
410 host.removeView(layout);
411 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
413 SharedPreferences pref = U.getSharedPreferences(context);
414 if(U.canDrawOverlays(context))
417 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
425 public void onDestroyHost(UIHost host) {
428 host.removeView(layout);
429 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
431 U.unregisterReceiver(context, toggleReceiver);
432 U.unregisterReceiver(context, addWidgetReceiver);
433 U.unregisterReceiver(context, removeWidgetReceiver);
434 U.unregisterReceiver(context, hideReceiver);
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);
441 pref.edit().remove(PREF_DONT_STOP_DASHBOARD).apply();
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);
449 int currentlySelectedCell = appWidgetId == -1 ? cellId : -1;
451 SharedPreferences pref = U.getSharedPreferences(context);
452 boolean shouldShowPlaceholder =
454 PREF_DASHBOARD_WIDGET_PREFIX
456 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
459 if(isActualClick && ((appWidgetId == -1 && currentlySelectedCell == previouslySelectedCell) || shouldShowPlaceholder)) {
462 FrameLayout frameLayout = cells.get(currentlySelectedCell);
463 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
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);
470 if(shouldShowPlaceholder) {
471 String providerName =
473 PREF_DASHBOARD_WIDGET_PREFIX
475 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
478 if(!providerName.equals("null")) {
479 ComponentName componentName = ComponentName.unflattenFromString(providerName);
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);
491 previouslySelectedCell = -1;
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);
498 previouslySelectedCell = currentlySelectedCell;
502 private void cellLongClick(View view) {
505 Bundle bundle = (Bundle) view.getTag();
506 int cellId = bundle.getInt(EXTRA_CELL_ID);
508 Intent intent = new Intent(ACTION_REMOVE_WIDGET_REQUESTED);
509 intent.putExtra(EXTRA_CELL_ID, cellId);
510 U.sendBroadcast(context, intent);
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);
519 addWidget(appWidgetId, cellId, true);
523 private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
524 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
526 final DashboardCell cellLayout = cells.get(cellId);
527 final AppWidgetHostView hostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
528 hostView.setAppWidget(appWidgetId, appWidgetInfo);
530 Bundle bundle = new Bundle();
531 bundle.putInt(EXTRA_CELL_ID, cellId);
532 hostView.setTag(bundle);
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);
540 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
541 linearLayout.addView(hostView);
543 Bundle bundle2 = (Bundle) cellLayout.getTag();
544 bundle2.putInt(EXTRA_APPWIDGET_ID, appWidgetId);
545 cellLayout.setTag(bundle2);
547 widgets.put(cellId, hostView);
550 saveWidgetInfo(context, appWidgetInfo, cellId, appWidgetId);
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());
563 void saveWidgetInfo(Context context,
564 AppWidgetProviderInfo appWidgetInfo,
567 SharedPreferences pref = U.getSharedPreferences(context);
568 SharedPreferences.Editor editor = pref.edit();
569 editor.putInt(PREF_DASHBOARD_WIDGET_PREFIX + cellId, appWidgetId);
571 PREF_DASHBOARD_WIDGET_PREFIX + cellId + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
572 appWidgetInfo.provider.flattenToString()
575 PREF_DASHBOARD_WIDGET_PREFIX
577 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX
582 private void removeWidget(int cellId, boolean tempRemove) {
583 widgets.remove(cellId);
585 DashboardCell cellLayout = cells.get(cellId);
586 Bundle bundle = (Bundle) cellLayout.getTag();
588 mAppWidgetHost.deleteAppWidgetId(bundle.getInt(EXTRA_APPWIDGET_ID));
589 bundle.remove(EXTRA_APPWIDGET_ID);
591 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
592 linearLayout.removeAllViews();
594 cellLayout.setTag(bundle);
595 cellLayout.setOnClickListener(cellOcl);
596 cellLayout.setOnHoverListener(cellOhl);
597 cellLayout.setOnLongClickListener(null);
598 cellLayout.setOnGenericMotionListener(null);
599 cellLayout.setOnInterceptedLongPressListener(null);
601 SharedPreferences pref = U.getSharedPreferences(context);
602 SharedPreferences.Editor editor = pref.edit();
603 editor.remove(PREF_DASHBOARD_WIDGET_PREFIX + cellId);
607 PREF_DASHBOARD_WIDGET_PREFIX
609 + PREF_DASHBOARD_WIDGET_PLACEHOLDER_SUFFIX,
612 addPlaceholder(cellId);
615 PREF_DASHBOARD_WIDGET_PREFIX
617 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX
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 =
628 PREF_DASHBOARD_WIDGET_PREFIX
630 + PREF_DASHBOARD_WIDGET_PROVIDER_SUFFIX,
634 if(!providerName.equals("null")) {
635 ImageView imageView = placeholder.findViewById(R.id.placeholder_image);
636 ComponentName componentName = ComponentName.unflattenFromString(providerName);
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);
644 ColorMatrix matrix = new ColorMatrix();
645 matrix.setSaturation(0);
647 imageView.setImageDrawable(drawable);
648 imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
654 placeholder.setVisibility(View.VISIBLE);