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.SharedPreferences;
31 import android.os.Bundle;
32 import android.view.KeyEvent;
33 import android.view.MotionEvent;
34 import android.view.WindowManager;
35 import android.widget.FrameLayout;
36 import android.widget.LinearLayout;
38 import com.farmerbb.taskbar.R;
39 import com.farmerbb.taskbar.helper.DashboardHelper;
40 import com.farmerbb.taskbar.util.DisplayInfo;
41 import com.farmerbb.taskbar.helper.LauncherHelper;
42 import com.farmerbb.taskbar.util.U;
44 import static com.farmerbb.taskbar.util.Constants.*;
46 public class DashboardActivity extends Activity {
48 private AppWidgetManager mAppWidgetManager;
49 private AppWidgetHost mAppWidgetHost;
51 private final int REQUEST_PICK_APPWIDGET = 456;
52 private final int REQUEST_CREATE_APPWIDGET = 789;
54 private boolean shouldFinish = true;
55 private boolean shouldCollapse = true;
56 private boolean contextMenuFix = false;
57 private int cellId = -1;
59 private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
61 public void onReceive(Context context, Intent intent) {
62 mAppWidgetManager = AppWidgetManager.getInstance(context);
63 mAppWidgetHost = new AppWidgetHost(context, intent.getIntExtra("appWidgetId", -1));
64 cellId = intent.getIntExtra("cellId", -1);
66 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
67 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
68 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
71 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
72 U.sendBroadcast(DashboardActivity.this, ACTION_TEMP_HIDE_TASKBAR);
73 } catch (ActivityNotFoundException e) {
74 U.showToast(DashboardActivity.this, R.string.tb_lock_device_not_supported);
82 private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
84 public void onReceive(final Context context, Intent intent) {
85 cellId = intent.getIntExtra("cellId", -1);
87 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
88 builder.setTitle(R.string.tb_remove_widget)
89 .setMessage(R.string.tb_are_you_sure)
90 .setNegativeButton(R.string.tb_action_cancel, (dialog, which) -> {
91 U.sendBroadcast(DashboardActivity.this, ACTION_REMOVE_WIDGET_COMPLETED);
95 .setPositiveButton(R.string.tb_action_ok, (dialog, which) -> {
96 Intent intent1 = new Intent(ACTION_REMOVE_WIDGET_COMPLETED);
97 intent1.putExtra("cellId", cellId);
98 U.sendBroadcast(DashboardActivity.this, intent1);
103 AlertDialog dialog = builder.create();
105 dialog.setCancelable(false);
107 shouldFinish = false;
111 private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
113 public void onReceive(Context context, Intent intent) {
114 shouldCollapse = false;
117 U.startFreeformHack(DashboardActivity.this);
124 protected void onCreate(Bundle savedInstanceState) {
125 super.onCreate(savedInstanceState);
127 contextMenuFix = getIntent().hasExtra("context_menu_fix");
129 // Detect outside touches, and finish the activity when one is detected
130 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
131 getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
133 DisplayInfo display = U.getDisplayInfo(this);
135 setContentView(R.layout.tb_incognito);
137 LinearLayout layout = findViewById(R.id.incognitoLayout);
138 layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
140 U.registerReceiver(this, addWidgetReceiver, ACTION_ADD_WIDGET_REQUESTED);
141 U.registerReceiver(this, removeWidgetReceiver, ACTION_REMOVE_WIDGET_REQUESTED);
142 U.registerReceiver(this, finishReceiver, ACTION_DASHBOARD_DISAPPEARING);
144 if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
148 public boolean onTouchEvent(MotionEvent event) {
149 if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
150 return super.onTouchEvent(event);
154 public void onBackPressed() {
156 U.startFreeformHack(this);
159 U.sendBroadcast(this, ACTION_HIDE_DASHBOARD);
163 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
164 if(event.getAction() == KeyEvent.ACTION_DOWN) {
169 return super.dispatchKeyShortcutEvent(event);
173 protected void onPause() {
178 if(U.shouldCollapse(this, true)) {
179 U.sendBroadcast(this, ACTION_HIDE_TASKBAR);
181 U.sendBroadcast(this, ACTION_HIDE_START_MENU);
185 contextMenuFix = false;
191 protected void onDestroy() {
194 U.unregisterReceiver(this, addWidgetReceiver);
195 U.unregisterReceiver(this, removeWidgetReceiver);
196 U.unregisterReceiver(this, finishReceiver);
200 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
201 if(resultCode == RESULT_OK) {
202 if(requestCode == REQUEST_PICK_APPWIDGET) {
203 configureWidget(data);
204 } else if(requestCode == REQUEST_CREATE_APPWIDGET) {
207 } else if(resultCode == RESULT_CANCELED) {
209 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
210 if(appWidgetId != -1) {
211 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
215 U.sendBroadcast(this, ACTION_ADD_WIDGET_COMPLETED);
216 U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);
222 private void configureWidget(Intent data) {
223 Bundle extras = data.getExtras();
224 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
225 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
226 if(appWidgetInfo.configure != null) {
227 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
228 intent.setComponent(appWidgetInfo.configure);
229 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
230 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
232 SharedPreferences pref = U.getSharedPreferences(this);
233 if(LauncherHelper.getInstance().isOnHomeScreen()
234 && (!pref.getBoolean(PREF_TASKBAR_ACTIVE, false)
235 || pref.getBoolean(PREF_IS_HIDDEN, false)))
236 pref.edit().putBoolean(PREF_DONT_STOP_DASHBOARD, true).apply();
238 shouldFinish = false;
244 private void createWidget(Intent data) {
245 Intent intent = new Intent(ACTION_ADD_WIDGET_COMPLETED);
246 intent.putExtra("appWidgetId", data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1));
247 intent.putExtra("cellId", cellId);
249 U.sendBroadcast(this, intent);
250 U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);