OSDN Git Service

d27da6a3c3641299b5301c7e8ca8c6790317d6b9
[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.Build;
32 import android.os.Bundle;
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;
38
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.helper.DashboardHelper;
41 import com.farmerbb.taskbar.util.DisplayInfo;
42 import com.farmerbb.taskbar.helper.LauncherHelper;
43 import com.farmerbb.taskbar.util.U;
44
45 import static com.farmerbb.taskbar.util.Constants.*;
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(EXTRA_APPWIDGET_ID, -1));
65             cellId = intent.getIntExtra(EXTRA_CELL_ID, -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                 U.sendBroadcast(DashboardActivity.this, ACTION_TEMP_HIDE_TASKBAR);
74             } catch (ActivityNotFoundException e) {
75                 U.showToast(DashboardActivity.this, R.string.tb_lock_device_not_supported);
76                 finish();
77             }
78
79             shouldFinish = false;
80         }
81     };
82
83     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
84         @Override
85         public void onReceive(final Context context, Intent intent) {
86             cellId = intent.getIntExtra(EXTRA_CELL_ID, -1);
87
88             AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
89             builder.setTitle(R.string.tb_remove_widget)
90                     .setMessage(R.string.tb_are_you_sure)
91                     .setNegativeButton(R.string.tb_action_cancel, (dialog, which) -> {
92                         U.sendBroadcast(DashboardActivity.this, ACTION_REMOVE_WIDGET_COMPLETED);
93
94                         shouldFinish = true;
95                     })
96                     .setPositiveButton(R.string.tb_action_ok, (dialog, which) -> {
97                         Intent intent1 = new Intent(ACTION_REMOVE_WIDGET_COMPLETED);
98                         intent1.putExtra(EXTRA_CELL_ID, cellId);
99                         U.sendBroadcast(DashboardActivity.this, intent1);
100
101                         shouldFinish = true;
102                     });
103
104             AlertDialog dialog = builder.create();
105             dialog.show();
106             dialog.setCancelable(false);
107
108             shouldFinish = false;
109         }
110     };
111
112     private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
113         @Override
114         public void onReceive(Context context, Intent intent) {
115             shouldCollapse = false;
116
117             if(contextMenuFix)
118                 U.startFreeformHack(DashboardActivity.this);
119
120             finish();
121         }
122     };
123
124     @Override
125     protected void onCreate(Bundle savedInstanceState) {
126         super.onCreate(savedInstanceState);
127
128         contextMenuFix = getIntent().hasExtra("context_menu_fix");
129
130         // Detect outside touches, and finish the activity when one is detected
131         getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
132         getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
133
134         DisplayInfo display = U.getDisplayInfo(this);
135
136         setContentView(R.layout.tb_incognito);
137
138         LinearLayout layout = findViewById(R.id.incognitoLayout);
139         layout.setLayoutParams(new FrameLayout.LayoutParams(display.width, display.height));
140
141         U.registerReceiver(this, addWidgetReceiver, ACTION_ADD_WIDGET_REQUESTED);
142         U.registerReceiver(this, removeWidgetReceiver, ACTION_REMOVE_WIDGET_REQUESTED);
143         U.registerReceiver(this, finishReceiver, ACTION_DASHBOARD_DISAPPEARING);
144
145         if(!DashboardHelper.getInstance().isDashboardOpen()) finish();
146     }
147
148     @Override
149     public boolean onTouchEvent(MotionEvent event) {
150         if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
151         return super.onTouchEvent(event);
152     }
153
154     @Override
155     public void onBackPressed() {
156         if(contextMenuFix) {
157             U.startFreeformHack(this);
158         }
159
160         U.sendBroadcast(this, ACTION_HIDE_DASHBOARD);
161     }
162
163     @Override
164     public boolean dispatchKeyShortcutEvent(KeyEvent event) {
165         if(event.getAction() == KeyEvent.ACTION_DOWN) {
166             event.getKeyCode();
167
168             return true;
169         }
170         return super.dispatchKeyShortcutEvent(event);
171     }
172
173  // @Override
174     public void onTopResumedActivityChanged(boolean isTopResumedActivity) {
175         if(!isTopResumedActivity)
176             performOnPauseLogic();
177     }
178
179     @Override
180     protected void onPause() {
181         super.onPause();
182
183         if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
184             performOnPauseLogic();
185     }
186
187     private void performOnPauseLogic() {
188         if(shouldFinish) {
189             if(shouldCollapse) {
190                 if(U.shouldCollapse(this, true)) {
191                     U.sendBroadcast(this, ACTION_HIDE_TASKBAR);
192                 } else {
193                     U.sendBroadcast(this, ACTION_HIDE_START_MENU);
194                 }
195             }
196
197             contextMenuFix = false;
198             onBackPressed();
199         }
200     }
201
202     @Override
203     protected void onDestroy() {
204         super.onDestroy();
205
206         U.unregisterReceiver(this, addWidgetReceiver);
207         U.unregisterReceiver(this, removeWidgetReceiver);
208         U.unregisterReceiver(this, finishReceiver);
209     }
210
211     @Override
212     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
213         if(resultCode == RESULT_OK) {
214             if(requestCode == REQUEST_PICK_APPWIDGET) {
215                 configureWidget(data);
216             } else if(requestCode == REQUEST_CREATE_APPWIDGET) {
217                 createWidget(data);
218             }
219         } else if(resultCode == RESULT_CANCELED) {
220             if(data != null) {
221                 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
222                 if(appWidgetId != -1) {
223                     mAppWidgetHost.deleteAppWidgetId(appWidgetId);
224                 }
225             }
226
227             U.sendBroadcast(this, ACTION_ADD_WIDGET_COMPLETED);
228             U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);
229
230             shouldFinish = true;
231         }
232     }
233
234     private void configureWidget(Intent data) {
235         Bundle extras = data.getExtras();
236         int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
237         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
238         if(appWidgetInfo.configure != null) {
239             Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
240             intent.setComponent(appWidgetInfo.configure);
241             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
242             startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
243
244             SharedPreferences pref = U.getSharedPreferences(this);
245             if(LauncherHelper.getInstance().isOnHomeScreen(this)
246                     && (!pref.getBoolean(PREF_TASKBAR_ACTIVE, false)
247                     || pref.getBoolean(PREF_IS_HIDDEN, false)))
248                 pref.edit().putBoolean(PREF_DONT_STOP_DASHBOARD, true).apply();
249
250             shouldFinish = false;
251         } else {
252             createWidget(data);
253         }
254     }
255
256     private void createWidget(Intent data) {
257         Intent intent = new Intent(ACTION_ADD_WIDGET_COMPLETED);
258         intent.putExtra(
259                 EXTRA_APPWIDGET_ID,
260                 data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
261         );
262         intent.putExtra(EXTRA_CELL_ID, cellId);
263
264         U.sendBroadcast(this, intent);
265         U.sendBroadcast(this, ACTION_TEMP_SHOW_TASKBAR);
266
267         shouldFinish = true;
268     }
269 }