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.activity;
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.appwidget.AppWidgetHost;
24 import android.appwidget.AppWidgetManager;
25 import android.appwidget.AppWidgetProviderInfo;
26 import android.content.ActivityNotFoundException;
27 import android.content.BroadcastReceiver;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.IntentFilter;
31 import android.content.SharedPreferences;
32 import android.os.Bundle;
33 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
34 import android.view.KeyEvent;
35 import android.view.MotionEvent;
36 import android.view.WindowManager;
37 import android.widget.FrameLayout;
38 import android.widget.LinearLayout;
40 import com.farmerbb.taskbar.R;
41 import com.farmerbb.taskbar.content.TaskbarIntent;
42 import com.farmerbb.taskbar.util.DashboardHelper;
43 import com.farmerbb.taskbar.util.DisplayInfo;
44 import com.farmerbb.taskbar.util.LauncherHelper;
45 import com.farmerbb.taskbar.util.U;
47 public class DashboardActivity extends Activity {
49 private AppWidgetManager mAppWidgetManager;
50 private AppWidgetHost mAppWidgetHost;
52 private final int REQUEST_PICK_APPWIDGET = 456;
53 private final int REQUEST_CREATE_APPWIDGET = 789;
55 private boolean shouldFinish = true;
56 private boolean shouldCollapse = true;
57 private boolean contextMenuFix = false;
58 private int cellId = -1;
60 private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
62 public void onReceive(Context context, Intent intent) {
63 mAppWidgetManager = AppWidgetManager.getInstance(context);
64 mAppWidgetHost = new AppWidgetHost(context, intent.getIntExtra("appWidgetId", -1));
65 cellId = intent.getIntExtra("cellId", -1);
67 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
68 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
69 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
72 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
74 .getInstance(DashboardActivity.this)
75 .sendBroadcast(new Intent(TaskbarIntent.ACTION_TEMP_HIDE_TASKBAR));
76 } catch (ActivityNotFoundException e) {
77 U.showToast(DashboardActivity.this, R.string.tb_lock_device_not_supported);
85 private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
87 public void onReceive(final Context context, Intent intent) {
88 cellId = intent.getIntExtra("cellId", -1);
90 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
91 builder.setTitle(R.string.tb_remove_widget)
92 .setMessage(R.string.tb_are_you_sure)
93 .setNegativeButton(R.string.tb_action_cancel, (dialog, which) -> {
95 .getInstance(DashboardActivity.this)
97 new Intent(TaskbarIntent.ACTION_REMOVE_WIDGET_COMPLETED)
102 .setPositiveButton(R.string.tb_action_ok, (dialog, which) -> {
103 Intent intent1 = new Intent(TaskbarIntent.ACTION_REMOVE_WIDGET_COMPLETED);
104 intent1.putExtra("cellId", cellId);
105 LocalBroadcastManager.getInstance(DashboardActivity.this).sendBroadcast(intent1);
110 AlertDialog dialog = builder.create();
112 dialog.setCancelable(false);
114 shouldFinish = false;
118 private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
120 public void onReceive(Context context, Intent intent) {
121 shouldCollapse = false;
124 U.startFreeformHack(DashboardActivity.this);
131 protected void onCreate(Bundle savedInstanceState) {
132 super.onCreate(savedInstanceState);
134 contextMenuFix = getIntent().hasExtra("context_menu_fix");
136 // Detect outside touches, and finish the activity when one is detected
137 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
138 getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
140 DisplayInfo display = U.getDisplayInfo(this);
142 setContentView(R.layout.tb_incognito);
144 LinearLayout layout = findViewById(R.id.incognitoLayout);
145 layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
147 LocalBroadcastManager
151 new IntentFilter(TaskbarIntent.ACTION_ADD_WIDGET_REQUESTED)
153 LocalBroadcastManager
156 removeWidgetReceiver,
157 new IntentFilter(TaskbarIntent.ACTION_REMOVE_WIDGET_REQUESTED)
159 LocalBroadcastManager
163 new IntentFilter(TaskbarIntent.ACTION_DASHBOARD_DISAPPEARING)
166 if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
170 public boolean onTouchEvent(MotionEvent event) {
171 if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
172 return super.onTouchEvent(event);
176 public void onBackPressed() {
178 U.startFreeformHack(this);
180 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_DASHBOARD"));
184 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
185 if(event.getAction() == KeyEvent.ACTION_DOWN) {
190 return super.dispatchKeyShortcutEvent(event);
194 protected void onPause() {
199 if(U.shouldCollapse(this, true)) {
200 LocalBroadcastManager
202 .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_TASKBAR));
204 LocalBroadcastManager
206 .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_START_MENU));
210 contextMenuFix = false;
216 protected void onDestroy() {
219 LocalBroadcastManager.getInstance(this).unregisterReceiver(addWidgetReceiver);
220 LocalBroadcastManager.getInstance(this).unregisterReceiver(removeWidgetReceiver);
221 LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
225 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
226 if(resultCode == RESULT_OK) {
227 if(requestCode == REQUEST_PICK_APPWIDGET) {
228 configureWidget(data);
229 } else if(requestCode == REQUEST_CREATE_APPWIDGET) {
232 } else if(resultCode == RESULT_CANCELED) {
234 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
235 if(appWidgetId != -1) {
236 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
240 LocalBroadcastManager
242 .sendBroadcast(new Intent(TaskbarIntent.ACTION_ADD_WIDGET_COMPLETED));
243 LocalBroadcastManager
245 .sendBroadcast(new Intent(TaskbarIntent.ACTION_TEMP_SHOW_TASKBAR));
251 private void configureWidget(Intent data) {
252 Bundle extras = data.getExtras();
253 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
254 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
255 if(appWidgetInfo.configure != null) {
256 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
257 intent.setComponent(appWidgetInfo.configure);
258 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
259 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
261 SharedPreferences pref = U.getSharedPreferences(this);
262 if(LauncherHelper.getInstance().isOnHomeScreen()
263 && (!pref.getBoolean("taskbar_active", false)
264 || pref.getBoolean("is_hidden", false)))
265 pref.edit().putBoolean("dont_stop_dashboard", true).apply();
267 shouldFinish = false;
273 private void createWidget(Intent data) {
274 Intent intent = new Intent(TaskbarIntent.ACTION_ADD_WIDGET_COMPLETED);
275 intent.putExtra("appWidgetId", data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1));
276 intent.putExtra("cellId", cellId);
278 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
279 lbm.sendBroadcast(intent);
280 lbm.sendBroadcast(new Intent(TaskbarIntent.ACTION_TEMP_SHOW_TASKBAR));