-
Notifications
You must be signed in to change notification settings - Fork 55
/
ShakeManager.java
173 lines (160 loc) · 5.82 KB
/
ShakeManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (C) 2017. Aesean
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aesean.activitystack.utils.shake;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.SensorManager;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.aesean.activitystack.BuildConfig;
import com.aesean.activitystack.utils.ApplicationUtils;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* ShakeManager
*
* @author xl
* @version V1.0
* @since 23/12/2016
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class ShakeManager implements ShakeDetector.ShakeListener {
private static final String TAG = "ShakeManager";
private ShakeDetector mShakeDetector;
private final Context mContext;
private final IGetActivity mGetActivity;
private boolean mShowing = false;
private long mLastShakeTime = -1;
private static final long SHAKE_DELAY = 1000;
public ShakeManager(final Context context) {
this(context, new IGetActivity() {
@Override
public Activity getActivity() {
return ApplicationUtils.getTopActivity();
}
});
}
public ShakeManager(Context context, IGetActivity getActivity) {
if (context == null) {
throw new NullPointerException("Context can not be null");
}
if (getActivity == null) {
throw new NullPointerException("IGetActivity can not be null");
}
mContext = context.getApplicationContext();
mGetActivity = getActivity;
}
public interface IGetActivity {
Activity getActivity();
}
public void unregisterShakeDetector() {
if (!BuildConfig.DEBUG) {
return;
}
if (mShakeDetector != null) {
mShakeDetector.stop();
}
}
public void registerShakeDetector() {
if (!BuildConfig.DEBUG) {
return;
}
if (mShakeDetector == null) {
mShakeDetector = new ShakeDetector(this);
}
mShakeDetector.start((SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE));
}
@Override
public void onShake() {
if (!BuildConfig.DEBUG) {
return;
}
if (mShowing) {
return;
}
long currentTimeMillis = System.currentTimeMillis();
if (mLastShakeTime != -1 && currentTimeMillis - mLastShakeTime < SHAKE_DELAY) {
return;
}
mLastShakeTime = currentTimeMillis;
final Activity activity = mGetActivity.getActivity();
if (activity == null) {
new Exception("Shake fail, getActivity is null").printStackTrace();
return;
}
final Map<String, Object> map = new LinkedHashMap<>();
if (activity instanceof IRegisterShakeDetector) {
((IRegisterShakeDetector) activity).registerShakeDetector(map);
}
if (activity instanceof FragmentActivity) {
@SuppressWarnings("RestrictedApi")
List<Fragment> fragments = ((FragmentActivity) activity).getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment instanceof IRegisterShakeDetector) {
((IRegisterShakeDetector) fragment).registerShakeDetector(map);
}
}
}
}
if (map.size() < 1) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("开发者菜单");
final String[] array = map.keySet().toArray(new String[map.size()]);
builder.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Object obj = map.get(array[which]);
if (obj instanceof Class) {
try {
// 检查是否是Activity的子类
Class clazz = (Class) obj;
if (Activity.class.isAssignableFrom(clazz)) {
Intent intent = new Intent(activity, clazz);
activity.startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (obj instanceof Intent) {
activity.startActivity((Intent) obj);
}
if (obj instanceof DialogInterface.OnClickListener) {
((DialogInterface.OnClickListener) obj).onClick(dialog, which);
}
if (obj instanceof Runnable) {
((Runnable) obj).run();
}
}
});
AlertDialog dialog = builder.create();
mShowing = true;
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mShowing = false;
}
});
dialog.show();
}
}