OSDN Git Service

Move helpers into their own package
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / DashboardActivity.java
1 /* Based on code by Leonardo Fischer
2  * See https://github.com/lgfischer/WidgetHostExample
3  *
4  * Copyright 2016 Braden Farmer
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package com.farmerbb.taskbar.activity;
20
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;
37
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;
43
44 import static com.farmerbb.taskbar.util.Constants.*;
45
46 public class DashboardActivity extends Activity {
47
48     private AppWidgetManager mAppWidgetManager;
49     private AppWidgetHost mAppWidgetHost;
50
51     private final int REQUEST_PICK_APPWIDGET = 456;
52     private final int REQUEST_CREATE_APPWIDGET = 789;
53
54     private boolean shouldFinish = true;
55     private boolean shouldCollapse = true;
56     private boolean contextMenuFix = false;
57     private int cellId = -1;
58
59     private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
60         @Override
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);
65
66             int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
67             Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
68             pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
69
70             try {
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);
75                 finish();
76             }
77
78             shouldFinish = false;
79         }
80     };
81
82     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
83         @Override
84         public void onReceive(final Context context, Intent intent) {
85             cellId = intent.getIntExtra("cellId", -1);
86
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);
92
93                         shouldFinish = true;
94                     })
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);
99
100                         shouldFinish = true;
101                     });
102
103             AlertDialog dialog = builder.create();
104             dialog.show();
105             dialog.setCancelable(false);
106
107             shouldFinish = false;
108         }
109     };
110
111     private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
112         @Override
113         public void onReceive(Context context, Intent intent) {
114             shouldCollapse = false;
115
116             if(contextMenuFix)
117                 U.startFreeformHack(DashboardActivity.this);
118
119             finish();
120         }
121     };
122
123     @Override
124     protected void onCreate(Bundle savedInstanceState) {
125         super.onCreate(savedInstanceState);
126
127         contextMenuFix = getIntent().hasExtra("context_menu_fix");
128
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);
132
133         DisplayInfo display = U.getDisplayInfo(this);
134
135         setContentView(R.layout.tb_incognito);
136
137         LinearLayout layout = findViewById(R.id.incognitoLayout);
138         layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
139
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);
143
144         if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
145     }
146
147     @Override
148     public boolean onTouchEvent(MotionEvent event) {
149         if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
150         return super.onTouchEvent(event);
151     }
152
153     @Override
154     public void onBackPressed() {
155         if(contextMenuFix) {
156             U.startFreeformHack(this);
157         }
158
159         U.sendBroadcast(this, ACTION_HIDE_DASHBOARD);
160     }
161
162     @Override
163     public boolean dispatchKeyShortcutEvent(KeyEvent event) {
164         if(event.getAction() == KeyEvent.ACTION_DOWN) {
165             event.getKeyCode();
166
167             return true;
168         }
169         return super.dispatchKeyShortcutEvent(event);
170     }
171
172     @Override
173     protected void onPause() {
174         super.onPause();
175
176         if(shouldFinish) {
177             if(shouldCollapse) {
178                 if(U.shouldCollapse(this, true)) {
179                     U.sendBroadcast(this, ACTION_HIDE_TASKBAR);
180                 } else {
181                     U.sendBroadcast(this, ACTION_HIDE_START_MENU);
182                 }
183             }
184
185             contextMenuFix = false;
186             onBackPressed();
187         }
188     }
189
190     @Override
191     protected void onDestroy() {
192         super.onDestroy();
193
194         U.unregisterReceiver(this, addWidgetReceiver);
195         U.unregisterReceiver(this, removeWidgetReceiver);
196         U.unregisterReceiver(this, finishReceiver);
197     }
198
199     @Override
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) {
205                 createWidget(data);
206             }
207         } else if(resultCode == RESULT_CANCELED) {
208             if(data != null) {
209                 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
210                 if(appWidgetId != -1) {
211                     mAppWidgetHost.deleteAppWidgetId(appWidgetId);
212                 }
213             }
214
215             U.sendBroadcast(this, ACTION_ADD_WIDGET_COMPLETED);
216             U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);
217
218             shouldFinish = true;
219         }
220     }
221
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);
231
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();
237
238             shouldFinish = false;
239         } else {
240             createWidget(data);
241         }
242     }
243
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);
248
249         U.sendBroadcast(this, intent);
250         U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);
251
252         shouldFinish = true;
253     }
254 }