OSDN Git Service

Extract DASHBOARD_DISAPPEARING to TaskbarIntent constant
[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.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;
39
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;
46
47 public class DashboardActivity extends Activity {
48
49     private AppWidgetManager mAppWidgetManager;
50     private AppWidgetHost mAppWidgetHost;
51
52     private final int REQUEST_PICK_APPWIDGET = 456;
53     private final int REQUEST_CREATE_APPWIDGET = 789;
54
55     private boolean shouldFinish = true;
56     private boolean shouldCollapse = true;
57     private boolean contextMenuFix = false;
58     private int cellId = -1;
59
60     private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
61         @Override
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);
66
67             int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
68             Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
69             pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
70
71             try {
72                 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
73                 LocalBroadcastManager
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);
78                 finish();
79             }
80
81             shouldFinish = false;
82         }
83     };
84
85     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
86         @Override
87         public void onReceive(final Context context, Intent intent) {
88             cellId = intent.getIntExtra("cellId", -1);
89
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) -> {
94                         LocalBroadcastManager
95                                 .getInstance(DashboardActivity.this)
96                                 .sendBroadcast(
97                                         new Intent(TaskbarIntent.ACTION_REMOVE_WIDGET_COMPLETED)
98                                 );
99
100                         shouldFinish = true;
101                     })
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);
106
107                         shouldFinish = true;
108                     });
109
110             AlertDialog dialog = builder.create();
111             dialog.show();
112             dialog.setCancelable(false);
113
114             shouldFinish = false;
115         }
116     };
117
118     private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
119         @Override
120         public void onReceive(Context context, Intent intent) {
121             shouldCollapse = false;
122
123             if(contextMenuFix)
124                 U.startFreeformHack(DashboardActivity.this);
125
126             finish();
127         }
128     };
129
130     @Override
131     protected void onCreate(Bundle savedInstanceState) {
132         super.onCreate(savedInstanceState);
133
134         contextMenuFix = getIntent().hasExtra("context_menu_fix");
135
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);
139
140         DisplayInfo display = U.getDisplayInfo(this);
141
142         setContentView(R.layout.tb_incognito);
143
144         LinearLayout layout = findViewById(R.id.incognitoLayout);
145         layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
146
147         LocalBroadcastManager
148                 .getInstance(this)
149                 .registerReceiver(
150                         addWidgetReceiver,
151                         new IntentFilter(TaskbarIntent.ACTION_ADD_WIDGET_REQUESTED)
152                 );
153         LocalBroadcastManager
154                 .getInstance(this)
155                 .registerReceiver(
156                         removeWidgetReceiver,
157                         new IntentFilter(TaskbarIntent.ACTION_REMOVE_WIDGET_REQUESTED)
158                 );
159         LocalBroadcastManager
160                 .getInstance(this)
161                 .registerReceiver(
162                         finishReceiver,
163                         new IntentFilter(TaskbarIntent.ACTION_DASHBOARD_DISAPPEARING)
164                 );
165
166         if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
167     }
168
169     @Override
170     public boolean onTouchEvent(MotionEvent event) {
171         if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
172         return super.onTouchEvent(event);
173     }
174
175     @Override
176     public void onBackPressed() {
177         if(contextMenuFix)
178             U.startFreeformHack(this);
179
180         LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_DASHBOARD"));
181     }
182
183     @Override
184     public boolean dispatchKeyShortcutEvent(KeyEvent event) {
185         if(event.getAction() == KeyEvent.ACTION_DOWN) {
186             event.getKeyCode();
187
188             return true;
189         }
190         return super.dispatchKeyShortcutEvent(event);
191     }
192
193     @Override
194     protected void onPause() {
195         super.onPause();
196
197         if(shouldFinish) {
198             if(shouldCollapse) {
199                 if(U.shouldCollapse(this, true)) {
200                     LocalBroadcastManager
201                             .getInstance(this)
202                             .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_TASKBAR));
203                 } else {
204                     LocalBroadcastManager
205                             .getInstance(this)
206                             .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_START_MENU));
207                 }
208             }
209
210             contextMenuFix = false;
211             onBackPressed();
212         }
213     }
214
215     @Override
216     protected void onDestroy() {
217         super.onDestroy();
218
219         LocalBroadcastManager.getInstance(this).unregisterReceiver(addWidgetReceiver);
220         LocalBroadcastManager.getInstance(this).unregisterReceiver(removeWidgetReceiver);
221         LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
222     }
223
224     @Override
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) {
230                 createWidget(data);
231             }
232         } else if(resultCode == RESULT_CANCELED) {
233             if(data != null) {
234                 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
235                 if(appWidgetId != -1) {
236                     mAppWidgetHost.deleteAppWidgetId(appWidgetId);
237                 }
238             }
239
240             LocalBroadcastManager
241                     .getInstance(this)
242                     .sendBroadcast(new Intent(TaskbarIntent.ACTION_ADD_WIDGET_COMPLETED));
243             LocalBroadcastManager
244                     .getInstance(this)
245                     .sendBroadcast(new Intent(TaskbarIntent.ACTION_TEMP_SHOW_TASKBAR));
246
247             shouldFinish = true;
248         }
249     }
250
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);
260
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();
266
267             shouldFinish = false;
268         } else {
269             createWidget(data);
270         }
271     }
272
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);
277
278         LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
279         lbm.sendBroadcast(intent);
280         lbm.sendBroadcast(new Intent(TaskbarIntent.ACTION_TEMP_SHOW_TASKBAR));
281
282         shouldFinish = true;
283     }
284 }