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.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;
59 import com.farmerbb.taskbar.R;
60 import com.farmerbb.taskbar.activity.DashboardActivity;
61 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
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;
69 import java.util.List;
71 public class DashboardController implements UIController {
73 private AppWidgetManager mAppWidgetManager;
74 private AppWidgetHost mAppWidgetHost;
76 private Context context;
77 private LinearLayout layout;
79 private SparseArray<DashboardCell> cells = new SparseArray<>();
80 private SparseArray<AppWidgetHostView> widgets = new SparseArray<>();
82 private final int APPWIDGET_HOST_ID = 123;
87 private int previouslySelectedCell = -1;
89 private View.OnClickListener ocl = view -> toggleDashboard();
91 private View.OnClickListener cellOcl = view -> cellClick(view, true);
93 private View.OnHoverListener cellOhl = (v, event) -> {
96 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
97 v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
102 private View.OnLongClickListener olcl = v -> {
107 private View.OnGenericMotionListener ogml = (view, motionEvent) -> {
108 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
109 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
115 private DashboardCell.OnInterceptedLongPressListener listener = this::cellLongClick;
117 private BroadcastReceiver toggleReceiver = new BroadcastReceiver() {
119 public void onReceive(Context context, Intent intent) {
124 private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
126 public void onReceive(Context context, Intent intent) {
129 if(intent.hasExtra("appWidgetId") && intent.hasExtra("cellId")) {
130 int appWidgetId = intent.getExtras().getInt("appWidgetId", -1);
131 int cellId = intent.getExtras().getInt("cellId", -1);
133 addWidget(appWidgetId, cellId, true);
138 private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
140 public void onReceive(Context context, Intent intent) {
143 if(intent.hasExtra("cellId")) {
144 int cellId = intent.getExtras().getInt("cellId", -1);
146 removeWidget(cellId, false);
151 private BroadcastReceiver hideReceiver = new BroadcastReceiver() {
153 public void onReceive(Context context, Intent intent) {
158 public DashboardController(Context context) {
159 this.context = context;
162 @TargetApi(Build.VERSION_CODES.M)
164 public void onCreateHost(UIHost 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))
171 pref.edit().putBoolean("taskbar_active", false).apply();
175 } else host.terminate();
176 } else host.terminate();
179 @SuppressLint("RtlHardcoded")
180 private void drawDashboard(UIHost host) {
181 final ViewParams params = new ViewParams(
182 WindowManager.LayoutParams.MATCH_PARENT,
183 WindowManager.LayoutParams.MATCH_PARENT,
185 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
189 layout = new LinearLayout(context);
190 layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
191 layout.setVisibility(View.GONE);
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));
198 boolean isPortrait = context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
199 boolean isLandscape = context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
211 maxSize = columns * rows;
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);
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);
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);
232 TextView empty = cellLayout.findViewById(R.id.empty);
233 empty.setBackgroundColor(accentColorAlt);
234 empty.setTextColor(accentColor);
236 Bundle bundle = new Bundle();
237 bundle.putInt("cellId", cellCount);
239 cellLayout.setTag(bundle);
240 cells.put(cellCount, cellLayout);
243 layout2.addView(cellLayout);
246 layout.addView(layout2);
249 mAppWidgetManager = AppWidgetManager.getInstance(context);
250 mAppWidgetHost = new AppWidgetHost(context, APPWIDGET_HOST_ID);
251 mAppWidgetHost.startListening();
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))
261 mAppWidgetHost.stopListening();
263 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
265 lbm.unregisterReceiver(toggleReceiver);
266 lbm.unregisterReceiver(addWidgetReceiver);
267 lbm.unregisterReceiver(removeWidgetReceiver);
268 lbm.unregisterReceiver(hideReceiver);
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"));
275 host.addView(layout, params);
277 new Handler().postDelayed(() -> {
278 int paddingSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size);
280 switch(U.getTaskbarPosition(context)) {
281 case "top_vertical_left":
282 case "bottom_vertical_left":
283 layout.setPadding(paddingSize, 0, 0, 0);
287 layout.setPadding(0, paddingSize, 0, 0);
289 case "top_vertical_right":
290 case "bottom_vertical_right":
291 layout.setPadding(0, 0, paddingSize, 0);
295 layout.setPadding(0, 0, 0, paddingSize);
301 private void toggleDashboard() {
302 if(layout.getVisibility() == View.GONE)
308 @TargetApi(Build.VERSION_CODES.N)
309 private void showDashboard() {
310 if(layout.getVisibility() == View.GONE) {
311 layout.setOnClickListener(ocl);
314 LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_APPEARING"));
315 LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
317 boolean inFreeformMode = FreeformHackHelper.getInstance().isInFreeformWorkspace();
319 final SharedPreferences pref = U.getSharedPreferences(context);
320 Intent intent = null;
322 switch(pref.getString("theme", "light")) {
324 intent = new Intent(context, DashboardActivity.class);
327 intent = new Intent(context, DashboardActivityDark.class);
332 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
333 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
337 if(intent != null && U.hasBrokenSetLaunchBoundsApi())
338 intent.putExtra("context_menu_fix", true);
340 U.startActivityMaximized(context, intent);
342 context.startActivity(intent);
344 for(int i = 0; i < maxSize; i++) {
345 final DashboardCell cellLayout = cells.get(i);
346 final AppWidgetHostView hostView = widgets.get(i);
348 if(hostView != null) {
350 context.getPackageManager().getApplicationInfo(hostView.getAppWidgetInfo().provider.getPackageName(), 0);
351 hostView.post(() -> {
352 ViewGroup.LayoutParams params = hostView.getLayoutParams();
353 params.width = cellLayout.getWidth();
354 params.height = cellLayout.getHeight();
355 hostView.setLayoutParams(params);
356 hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
358 } catch (PackageManager.NameNotFoundException e) {
359 removeWidget(i, false);
360 } catch (NullPointerException e) {
361 removeWidget(i, true);
366 if(!pref.getBoolean("dashboard_tutorial_shown", false)) {
367 U.showToastLong(context, R.string.dashboard_tutorial);
368 pref.edit().putBoolean("dashboard_tutorial_shown", true).apply();
373 private void hideDashboard() {
374 if(layout.getVisibility() == View.VISIBLE) {
375 layout.setOnClickListener(null);
378 for(int i = 0; i < maxSize; i++) {
379 FrameLayout frameLayout = cells.get(i);
380 frameLayout.findViewById(R.id.empty).setVisibility(View.GONE);
383 previouslySelectedCell = -1;
387 private void fadeIn() {
388 mAppWidgetHost.startListening();
390 DashboardHelper.getInstance().setDashboardOpen(true);
392 layout.setVisibility(View.VISIBLE);
395 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
399 private void fadeOut(final boolean sendIntent) {
400 mAppWidgetHost.stopListening();
402 DashboardHelper.getInstance().setDashboardOpen(false);
406 .setDuration(context.getResources().getInteger(android.R.integer.config_shortAnimTime))
407 .setListener(new AnimatorListenerAdapter() {
409 public void onAnimationEnd(Animator animation) {
410 layout.setVisibility(View.GONE);
411 if(sendIntent) LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING"));
416 @TargetApi(Build.VERSION_CODES.M)
418 public void onRecreateHost(UIHost host) {
421 host.removeView(layout);
422 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
424 SharedPreferences pref = U.getSharedPreferences(context);
425 if(U.canDrawOverlays(context))
428 pref.edit().putBoolean("taskbar_active", false).apply();
436 public void onDestroyHost(UIHost host) {
439 host.removeView(layout);
440 } catch (IllegalArgumentException e) { /* Gracefully fail */ }
442 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
444 lbm.unregisterReceiver(toggleReceiver);
445 lbm.unregisterReceiver(addWidgetReceiver);
446 lbm.unregisterReceiver(removeWidgetReceiver);
447 lbm.unregisterReceiver(hideReceiver);
449 lbm.sendBroadcast(new Intent("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING"));
452 private void cellClick(View view, boolean isActualClick) {
453 Bundle bundle = (Bundle) view.getTag();
454 int cellId = bundle.getInt("cellId");
455 int appWidgetId = bundle.getInt("appWidgetId", -1);
457 int currentlySelectedCell = appWidgetId == -1 ? cellId : -1;
459 SharedPreferences pref = U.getSharedPreferences(context);
460 boolean shouldShowPlaceholder = pref.getBoolean("dashboard_widget_" + cellId + "_placeholder", false);
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("com.farmerbb.taskbar.ADD_WIDGET_REQUESTED");
468 intent.putExtra("appWidgetId", APPWIDGET_HOST_ID);
469 intent.putExtra("cellId", cellId);
470 LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
472 if(shouldShowPlaceholder) {
473 String providerName = pref.getString("dashboard_widget_" + cellId + "_provider", "null");
474 if(!providerName.equals("null")) {
475 ComponentName componentName = ComponentName.unflattenFromString(providerName);
477 List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
478 for(AppWidgetProviderInfo info : providerInfoList) {
479 if(info.provider.equals(componentName)) {
480 U.showToast(context, context.getString(R.string.widget_restore_toast, info.loadLabel(context.getPackageManager())), Toast.LENGTH_SHORT);
487 previouslySelectedCell = -1;
489 for(int i = 0; i < maxSize; i++) {
490 FrameLayout frameLayout = cells.get(i);
491 frameLayout.findViewById(R.id.empty).setVisibility(i == currentlySelectedCell && !shouldShowPlaceholder ? View.VISIBLE : View.GONE);
494 previouslySelectedCell = currentlySelectedCell;
498 private void cellLongClick(View view) {
501 Bundle bundle = (Bundle) view.getTag();
502 int cellId = bundle.getInt("cellId");
504 Intent intent = new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_REQUESTED");
505 intent.putExtra("cellId", cellId);
506 LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
509 private void addWidget(int appWidgetId, int cellId, boolean shouldSave) {
510 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
512 final DashboardCell cellLayout = cells.get(cellId);
513 final AppWidgetHostView hostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
514 hostView.setAppWidget(appWidgetId, appWidgetInfo);
516 Bundle bundle = new Bundle();
517 bundle.putInt("cellId", cellId);
518 hostView.setTag(bundle);
520 cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
521 cellLayout.findViewById(R.id.placeholder).setVisibility(View.GONE);
522 cellLayout.setOnLongClickListener(olcl);
523 cellLayout.setOnGenericMotionListener(ogml);
524 cellLayout.setOnInterceptedLongPressListener(listener);
526 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
527 linearLayout.addView(hostView);
529 Bundle bundle2 = (Bundle) cellLayout.getTag();
530 bundle2.putInt("appWidgetId", appWidgetId);
531 cellLayout.setTag(bundle2);
533 widgets.put(cellId, hostView);
536 SharedPreferences pref = U.getSharedPreferences(context);
537 SharedPreferences.Editor editor = pref.edit();
538 editor.putInt("dashboard_widget_" + cellId, appWidgetId);
539 editor.putString("dashboard_widget_" + cellId + "_provider", appWidgetInfo.provider.flattenToString());
540 editor.remove("dashboard_widget_" + cellId + "_placeholder");
544 new Handler().post(() -> {
545 ViewGroup.LayoutParams params = hostView.getLayoutParams();
546 params.width = cellLayout.getWidth();
547 params.height = cellLayout.getHeight();
548 hostView.setLayoutParams(params);
549 hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
553 private void removeWidget(int cellId, boolean tempRemove) {
554 widgets.remove(cellId);
556 DashboardCell cellLayout = cells.get(cellId);
557 Bundle bundle = (Bundle) cellLayout.getTag();
559 mAppWidgetHost.deleteAppWidgetId(bundle.getInt("appWidgetId"));
560 bundle.remove("appWidgetId");
562 LinearLayout linearLayout = cellLayout.findViewById(R.id.dashboard);
563 linearLayout.removeAllViews();
565 cellLayout.setTag(bundle);
566 cellLayout.setOnClickListener(cellOcl);
567 cellLayout.setOnHoverListener(cellOhl);
568 cellLayout.setOnLongClickListener(null);
569 cellLayout.setOnGenericMotionListener(null);
570 cellLayout.setOnInterceptedLongPressListener(null);
572 SharedPreferences pref = U.getSharedPreferences(context);
573 SharedPreferences.Editor editor = pref.edit();
574 editor.remove("dashboard_widget_" + cellId);
577 editor.putBoolean("dashboard_widget_" + cellId + "_placeholder", true);
578 addPlaceholder(cellId);
580 editor.remove("dashboard_widget_" + cellId + "_provider");
585 private void addPlaceholder(int cellId) {
586 FrameLayout placeholder = cells.get(cellId).findViewById(R.id.placeholder);
587 SharedPreferences pref = U.getSharedPreferences(context);
588 String providerName = pref.getString("dashboard_widget_" + cellId + "_provider", "null");
590 if(!providerName.equals("null")) {
591 ImageView imageView = placeholder.findViewById(R.id.placeholder_image);
592 ComponentName componentName = ComponentName.unflattenFromString(providerName);
594 List<AppWidgetProviderInfo> providerInfoList = mAppWidgetManager.getInstalledProvidersForProfile(Process.myUserHandle());
595 for(AppWidgetProviderInfo info : providerInfoList) {
596 if(info.provider.equals(componentName)) {
597 Drawable drawable = info.loadPreviewImage(context, -1);
598 if(drawable == null) drawable = info.loadIcon(context, -1);
600 ColorMatrix matrix = new ColorMatrix();
601 matrix.setSaturation(0);
603 imageView.setImageDrawable(drawable);
604 imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
610 placeholder.setVisibility(View.VISIBLE);