|
1
|
1
|
package com.studymachine.www.app;
|
|
2
|
2
|
|
|
|
3
|
+import android.app.Activity;
|
|
|
4
|
+import android.app.AppOpsManager;
|
|
3
|
5
|
import android.content.Context;
|
|
4
|
6
|
import android.content.Intent;
|
|
5
|
7
|
import android.hardware.display.DisplayManager;
|
|
6
|
8
|
import android.hardware.usb.UsbDevice;
|
|
|
9
|
+import android.net.Uri;
|
|
|
10
|
+import android.os.Binder;
|
|
7
|
11
|
import android.os.Build;
|
|
8
|
12
|
import android.os.Bundle;
|
|
|
13
|
+import android.provider.Settings;
|
|
9
|
14
|
import android.view.Display;
|
|
10
|
15
|
import android.view.View;
|
|
11
|
16
|
import android.view.WindowManager;
|
|
...
|
...
|
@@ -40,6 +45,9 @@ import com.studymachine.www.ui.presentation.WebPresentation; |
|
40
|
45
|
|
|
41
|
46
|
import org.greenrobot.eventbus.EventBus;
|
|
42
|
47
|
|
|
|
48
|
+import java.lang.reflect.Field;
|
|
|
49
|
+import java.lang.reflect.Method;
|
|
|
50
|
+
|
|
43
|
51
|
import okhttp3.Call;
|
|
44
|
52
|
|
|
45
|
53
|
/**
|
|
...
|
...
|
@@ -71,7 +79,8 @@ public abstract class AppActivity extends BaseActivity |
|
71
|
79
|
*/
|
|
72
|
80
|
private int mDialogCount;
|
|
73
|
81
|
|
|
74
|
|
- public GlassInfo deviceInfo = RKGlassDevice.getInstance().getGlassInfo();;
|
|
|
82
|
+ public GlassInfo deviceInfo = RKGlassDevice.getInstance().getGlassInfo();
|
|
|
83
|
+ ;
|
|
75
|
84
|
|
|
76
|
85
|
MutableLiveData<Boolean> connectStatus = new MutableLiveData();
|
|
77
|
86
|
|
|
...
|
...
|
@@ -131,30 +140,69 @@ public abstract class AppActivity extends BaseActivity |
|
131
|
140
|
mDialog.dismiss();
|
|
132
|
141
|
}
|
|
133
|
142
|
|
|
|
143
|
+ //判断是否开启悬浮窗权限 context可以用你的Activity.或者tiis
|
|
|
144
|
+ public static boolean checkFloatPermission(Context context) {
|
|
|
145
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
|
|
|
146
|
+ return true;
|
|
|
147
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
148
|
+ try {
|
|
|
149
|
+ Class cls = Class.forName("android.content.Context");
|
|
|
150
|
+ Field declaredField = cls.getDeclaredField("APP_OPS_SERVICE");
|
|
|
151
|
+ declaredField.setAccessible(true);
|
|
|
152
|
+ Object obj = declaredField.get(cls);
|
|
|
153
|
+ if (!(obj instanceof String)) {
|
|
|
154
|
+ return false;
|
|
|
155
|
+ }
|
|
|
156
|
+ String str2 = (String) obj;
|
|
|
157
|
+ obj = cls.getMethod("getSystemService", String.class).invoke(context, str2);
|
|
|
158
|
+ cls = Class.forName("android.app.AppOpsManager");
|
|
|
159
|
+ Field declaredField2 = cls.getDeclaredField("MODE_ALLOWED");
|
|
|
160
|
+ declaredField2.setAccessible(true);
|
|
|
161
|
+ Method checkOp = cls.getMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class);
|
|
|
162
|
+ int result = (Integer) checkOp.invoke(obj, 24, Binder.getCallingUid(), context.getPackageName());
|
|
|
163
|
+ return result == declaredField2.getInt(cls);
|
|
|
164
|
+ } catch (Exception e) {
|
|
|
165
|
+ return false;
|
|
|
166
|
+ }
|
|
|
167
|
+ } else {
|
|
|
168
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
169
|
+ AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
|
|
|
170
|
+ if (appOpsMgr == null)
|
|
|
171
|
+ return false;
|
|
|
172
|
+ int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), context
|
|
|
173
|
+ .getPackageName());
|
|
|
174
|
+ return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
|
|
|
175
|
+ } else {
|
|
|
176
|
+ return Settings.canDrawOverlays(context);
|
|
|
177
|
+ }
|
|
|
178
|
+ }
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ private void requestSettingCanDrawOverlays() {
|
|
|
182
|
+ int sdkInt = Build.VERSION.SDK_INT;
|
|
|
183
|
+ if (sdkInt >= Build.VERSION_CODES.O) {//8.0以上
|
|
|
184
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
|
|
|
185
|
+ startActivity(intent);
|
|
|
186
|
+ } else if (sdkInt >= Build.VERSION_CODES.M) {//6.0-8.0
|
|
|
187
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
|
|
|
188
|
+ intent.setData(Uri.parse("package:" + getPackageName()));
|
|
|
189
|
+ startActivity(intent);
|
|
|
190
|
+ } else {//4.4-6.0以下
|
|
|
191
|
+ //无需处理了
|
|
|
192
|
+ }
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
134
|
195
|
@Override
|
|
135
|
196
|
protected void initLayout() {
|
|
136
|
|
- if(!RKGlassDevice.getInstance().requestUSBDevicePermission()){
|
|
|
197
|
+ if (!checkFloatPermission(this)) {
|
|
|
198
|
+ //权限请求方法
|
|
|
199
|
+ requestSettingCanDrawOverlays();
|
|
|
200
|
+ }
|
|
|
201
|
+ if (!RKGlassDevice.getInstance().requestUSBDevicePermission()) {
|
|
137
|
202
|
toast("请到系统设置添加悬浮框权限");
|
|
138
|
203
|
finish();
|
|
139
|
204
|
}
|
|
140
|
|
- RKGlassDevice.getInstance().init(new OnGlassDeviceConnectListener() {
|
|
141
|
|
- @Override
|
|
142
|
|
- public void onGlassDeviceConnected(UsbDevice usbDevice) {
|
|
143
|
|
-// //Glass 设备连接成功
|
|
144
|
|
-// addControllerView();
|
|
145
|
|
- connectStatus.setValue(true);
|
|
146
|
|
- openAppPresentation();
|
|
147
|
|
- }
|
|
148
|
|
-
|
|
149
|
|
- @Override
|
|
150
|
|
- public void onGlassDeviceDisconnected() {
|
|
151
|
|
- connectStatus.setValue(false);
|
|
152
|
|
- ActivityManager.getInstance().finishAllActivities();
|
|
153
|
|
-// mControllerBid.setVisibility(View.GONE);
|
|
154
|
|
- }
|
|
155
|
|
- });
|
|
156
|
205
|
|
|
157
|
|
- deviceInfo = RKGlassDevice.getInstance().getGlassInfo();
|
|
158
|
206
|
super.initLayout();
|
|
159
|
207
|
if (getTitleBar() != null) {
|
|
160
|
208
|
getTitleBar().setOnTitleBarListener(this);
|
|
...
|
...
|
@@ -172,6 +220,30 @@ public abstract class AppActivity extends BaseActivity |
|
172
|
220
|
//保持常亮
|
|
173
|
221
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
174
|
222
|
EventBusManager.register(this);
|
|
|
223
|
+ RKGlassDevice.getInstance().init(new OnGlassDeviceConnectListener() {
|
|
|
224
|
+ @Override
|
|
|
225
|
+ public void onGlassDeviceConnected(UsbDevice usbDevice) {
|
|
|
226
|
+// //Glass 设备连接成功
|
|
|
227
|
+// addControllerView();
|
|
|
228
|
+ if (isDestroyed()){
|
|
|
229
|
+ return;
|
|
|
230
|
+ }
|
|
|
231
|
+ connectStatus.setValue(true);
|
|
|
232
|
+ openAppPresentation();
|
|
|
233
|
+ }
|
|
|
234
|
+
|
|
|
235
|
+ @Override
|
|
|
236
|
+ public void onGlassDeviceDisconnected() {
|
|
|
237
|
+ if (isDestroyed()){
|
|
|
238
|
+ return;
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ connectStatus.setValue(false);
|
|
|
242
|
+ ActivityManager.getInstance().finishAllActivities();
|
|
|
243
|
+// mControllerBid.setVisibility(View.GONE);
|
|
|
244
|
+ }
|
|
|
245
|
+ });
|
|
|
246
|
+ deviceInfo = RKGlassDevice.getInstance().getGlassInfo();
|
|
175
|
247
|
}
|
|
176
|
248
|
|
|
177
|
249
|
/**
|
|
...
|
...
|
@@ -289,6 +361,12 @@ public abstract class AppActivity extends BaseActivity |
|
289
|
361
|
}
|
|
290
|
362
|
|
|
291
|
363
|
@Override
|
|
|
364
|
+ protected void onResume() {
|
|
|
365
|
+ RKGlassDevice.getInstance().setDisplayMode(RKGlassDevice.GlassDisplayMode.MODE_2D);
|
|
|
366
|
+ super.onResume();
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ @Override
|
|
292
|
370
|
protected void onDestroy() {
|
|
293
|
371
|
super.onDestroy();
|
|
294
|
372
|
if (isShowDialog()) {
|
...
|
...
|
|