OSDN Git Service

f8e0ad7cf8484830e4522786ccf2ea1aef63bbc8
[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.DialogInterface;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.hardware.display.DisplayManager;
33 import android.os.Bundle;
34 import android.support.v4.content.LocalBroadcastManager;
35 import android.view.Display;
36 import android.view.KeyEvent;
37 import android.view.MotionEvent;
38 import android.view.WindowManager;
39 import android.widget.FrameLayout;
40 import android.widget.LinearLayout;
41
42 import com.farmerbb.taskbar.R;
43 import com.farmerbb.taskbar.util.U;
44
45 public class DashboardActivity extends Activity {
46
47     private AppWidgetManager mAppWidgetManager;
48     private AppWidgetHost mAppWidgetHost;
49
50     int REQUEST_PICK_APPWIDGET = 456;
51     int REQUEST_CREATE_APPWIDGET = 789;
52
53     boolean shouldFinish = true;
54
55     int cellId = -1;
56
57     private BroadcastReceiver addWidgetReceiver = new BroadcastReceiver() {
58         @Override
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);
63
64             int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
65             Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
66             pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
67
68             try {
69                 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
70             } catch (ActivityNotFoundException e) {
71                 U.showToast(DashboardActivity.this, R.string.lock_device_not_supported);
72                 finish();
73             }
74
75             shouldFinish = false;
76         }
77     };
78
79     private BroadcastReceiver removeWidgetReceiver = new BroadcastReceiver() {
80         @Override
81         public void onReceive(final Context context, Intent intent) {
82             cellId = intent.getIntExtra("cellId", -1);
83
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, new DialogInterface.OnClickListener() {
88                         @Override
89                         public void onClick(DialogInterface dialog, int which) {
90                             LocalBroadcastManager.getInstance(DashboardActivity.this).sendBroadcast(new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED"));
91
92                             shouldFinish = true;
93                         }
94                     })
95                     .setPositiveButton(R.string.action_ok, new DialogInterface.OnClickListener() {
96                         @Override
97                         public void onClick(DialogInterface dialog, int which) {
98                             Intent intent = new Intent("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED");
99                             intent.putExtra("cellId", cellId);
100                             LocalBroadcastManager.getInstance(DashboardActivity.this).sendBroadcast(intent);
101
102                             shouldFinish = true;
103                         }
104                     });
105
106             AlertDialog dialog = builder.create();
107             dialog.show();
108             dialog.setCancelable(false);
109
110             shouldFinish = false;
111         }
112     };
113
114     private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
115         @Override
116         public void onReceive(Context context, Intent intent) {
117             finish();
118         }
119     };
120
121     @SuppressWarnings("deprecation")
122     @Override
123     protected void onCreate(Bundle savedInstanceState) {
124         super.onCreate(savedInstanceState);
125
126         // Detect outside touches, and finish the activity when one is detected
127         getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
128         getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
129
130         DisplayManager dm = (DisplayManager) getSystemService(DISPLAY_SERVICE);
131         Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
132
133         setContentView(R.layout.incognito);
134
135         LinearLayout layout = (LinearLayout) findViewById(R.id.incognitoLayout);
136         layout.setLayoutParams(new FrameLayout.LayoutParams(display.getWidth(), display.getHeight()));
137
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"));
141     }
142
143     @Override
144     public boolean onTouchEvent(MotionEvent event) {
145         if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_OUTSIDE) onBackPressed();
146         return super.onTouchEvent(event);
147     }
148
149     @Override
150     public void onBackPressed() {
151         LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_DASHBOARD"));
152     }
153
154     @Override
155     public boolean dispatchKeyShortcutEvent(KeyEvent event) {
156         if(event.getAction() == KeyEvent.ACTION_DOWN) {
157             event.getKeyCode();
158
159             return true;
160         }
161         return super.dispatchKeyShortcutEvent(event);
162     }
163
164     @Override
165     protected void onPause() {
166         super.onPause();
167
168         if(shouldFinish)
169             onBackPressed();
170     }
171
172     @Override
173     protected void onDestroy() {
174         super.onDestroy();
175
176         LocalBroadcastManager.getInstance(this).unregisterReceiver(addWidgetReceiver);
177         LocalBroadcastManager.getInstance(this).unregisterReceiver(removeWidgetReceiver);
178         LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
179     }
180
181     @Override
182     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
183         if(resultCode == RESULT_OK) {
184             if(requestCode == REQUEST_PICK_APPWIDGET) {
185                 configureWidget(data);
186             } else if(requestCode == REQUEST_CREATE_APPWIDGET) {
187                 createWidget(data);
188             }
189         } else if(resultCode == RESULT_CANCELED && data != null) {
190             int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
191             if(appWidgetId != -1) {
192                 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
193             }
194
195             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED"));
196
197             shouldFinish = true;
198         }
199     }
200
201     private void configureWidget(Intent data) {
202         Bundle extras = data.getExtras();
203         int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
204         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
205         if(appWidgetInfo.configure != null) {
206             Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
207             intent.setComponent(appWidgetInfo.configure);
208             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
209             startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
210
211             shouldFinish = false;
212         } else {
213             createWidget(data);
214         }
215     }
216
217     private void createWidget(Intent data) {
218         Intent intent = new Intent("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED");
219         intent.putExtra("appWidgetId", data.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1));
220         intent.putExtra("cellId", cellId);
221         LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
222
223         shouldFinish = true;
224     }
225 }