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.os.Bundle;
32 import android.support.v4.content.LocalBroadcastManager;
33 import android.view.KeyEvent;
34 import android.view.MotionEvent;
35 import android.view.WindowManager;
36 import android.widget.FrameLayout;
37 import android.widget.LinearLayout;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.util.DashboardHelper;
41 import com.farmerbb.taskbar.util.DisplayInfo;
42 import com.farmerbb.taskbar.util.U;
44 public class DashboardActivity extends Activity {
46 private AppWidgetManager mAppWidgetManager;
47 private AppWidgetHost mAppWidgetHost;
49 private final int REQUEST_PICK_APPWIDGET = 456;
50 private final int REQUEST_CREATE_APPWIDGET = 789;
52 private boolean shouldFinish = true;
53 private boolean shouldCollapse = true;
54 private boolean contextMenuFix = false;
55 private int cellId = -1;
57 private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
59 public void onReceive(Context context, Intent intent) {
60 mAppWidgetManager = AppWidgetManager.getInstance(context);
61 mAppWidgetHost = new AppWidgetHost(context, intent.getIntExtra("appWidgetId", -1));
62 cellId = intent.getIntExtra("cellId", -1);
64 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
65 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
66 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
69 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
70 } catch (ActivityNotFoundException e) {
71 U.showToast(DashboardActivity.this, R.string.lock_device_not_supported);
79 private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
81 public void onReceive(final Context context, Intent intent) {
82 cellId = intent.getIntExtra("cellId", -1);
84 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
85 builder.setTitle(R.string.remove_widget)
86 .setMessage(R.string.are_you_sure)
87 .setNegativeButton(R.string.action_cancel, (dialog, which) -> {
88 LocalBroadcastManager.getInstance(DashboardActivity.this).sendBroadcast(new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED"));
92 .setPositiveButton(R.string.action_ok, (dialog, which) -> {
93 Intent intent1 = new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED");
94 intent1.putExtra("cellId", cellId);
95 LocalBroadcastManager.getInstance(DashboardActivity.this).sendBroadcast(intent1);
100 AlertDialog dialog = builder.create();
102 dialog.setCancelable(false);
104 shouldFinish = false;
108 private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
110 public void onReceive(Context context, Intent intent) {
111 shouldCollapse = false;
114 U.startFreeformHack(DashboardActivity.this);
120 @SuppressWarnings("deprecation")
122 protected void onCreate(Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
125 contextMenuFix = getIntent().hasExtra("context_menu_fix");
127 // Detect outside touches, and finish the activity when one is detected
128 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
129 getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
131 DisplayInfo display = U.getDisplayInfo(this);
133 setContentView(R.layout.incognito);
135 LinearLayout layout = findViewById(R.id.incognitoLayout);
136 layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
138 LocalBroadcastManager.getInstance(this).registerReceiver(addWidgetReceiver, new IntentFilter("com.farmerbb.taskbar.ADD_WIDGET_REQUESTED"));
139 LocalBroadcastManager.getInstance(this).registerReceiver(removeWidgetReceiver, new IntentFilter("com.farmerbb.taskbar.REMOVE_WIDGET_REQUESTED"));
140 LocalBroadcastManager.getInstance(this).registerReceiver(finishReceiver, new IntentFilter("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING"));
142 if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
146 public boolean onTouchEvent(MotionEvent event) {
147 if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
148 return super.onTouchEvent(event);
152 public void onBackPressed() {
154 U.startFreeformHack(this);
156 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_DASHBOARD"));
160 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
161 if(event.getAction() == KeyEvent.ACTION_DOWN) {
166 return super.dispatchKeyShortcutEvent(event);
170 protected void onPause() {
175 if(U.shouldCollapse(this, true))
176 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
178 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
181 contextMenuFix = false;
187 protected void onDestroy() {
190 LocalBroadcastManager.getInstance(this).unregisterReceiver(addWidgetReceiver);
191 LocalBroadcastManager.getInstance(this).unregisterReceiver(removeWidgetReceiver);
192 LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
196 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
197 if(resultCode == RESULT_OK) {
198 if(requestCode == REQUEST_PICK_APPWIDGET) {
199 configureWidget(data);
200 } else if(requestCode == REQUEST_CREATE_APPWIDGET) {
203 } else if(resultCode == RESULT_CANCELED) {
205 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
206 if(appWidgetId != -1) {
207 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
211 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED"));
217 private void configureWidget(Intent data) {
218 Bundle extras = data.getExtras();
219 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
220 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
221 if(appWidgetInfo.configure != null) {
222 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
223 intent.setComponent(appWidgetInfo.configure);
224 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
225 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
227 shouldFinish = false;
233 private void createWidget(Intent data) {
234 Intent intent = new Intent("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED");
235 intent.putExtra("appWidgetId", data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1));
236 intent.putExtra("cellId", cellId);
237 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);