-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainActivity.java
135 lines (115 loc) · 4.9 KB
/
MainActivity.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
package com.d.permissioncompat;
import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.d.lib.permissioncompat.Permission;
import com.d.lib.permissioncompat.PermissionCompat;
import com.d.lib.permissioncompat.PermissionSchedulers;
import com.d.lib.permissioncompat.callback.WeakRefPermissionCallback;
import com.d.lib.permissioncompat.callback.WeakRefPermissionSimpleCallback;
import java.lang.reflect.Field;
public class MainActivity extends AppCompatActivity {
private static String[] PERMISSIONS = {
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.GET_ACCOUNTS,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.PROCESS_OUTGOING_CALLS,
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR,
Manifest.permission.BODY_SENSORS,
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_WAP_PUSH,
Manifest.permission.RECEIVE_MMS,
Manifest.permission.RECEIVE_SMS,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PermissionCompat.checkSelfPermissions(this, new WeakRefSimpleCallback(this), PERMISSIONS);
findViewById(R.id.btn_permission).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions();
}
});
}
private void requestPermissions() {
PermissionCompat.with(this).requestEachCombined(PERMISSIONS)
.subscribeOn(PermissionSchedulers.io())
.observeOn(PermissionSchedulers.mainThread())
.requestPermissions(new WeakRefCallback(this));
}
private void test() {
WeakRefCallback callback = new WeakRefCallback(this);
callback.onNext(new Permission("", true, false));
Class clazz = callback.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
Log.v("PermissionCompat", "dsiner " + field.getName());
try {
Log.v("PermissionCompat", "dsiner " + field.get(callback));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
static class WeakRefCallback extends WeakRefPermissionCallback<MainActivity> {
public WeakRefCallback(MainActivity view) {
super(view);
}
@Override
public void onNext(Permission permission) {
if (isFinish()) {
Log.v("PermissionCompat", "dsiner permission onNext isFinish");
return;
}
Log.v("PermissionCompat", "dsiner permission onNext");
if (permission.granted) {
// All permissions are granted !
Toast.makeText(getView().getApplicationContext(), "All permissions are granted", Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale) {
// At least one denied permission without ask never again
Toast.makeText(getView().getApplicationContext(), "Permission without ask never again", Toast.LENGTH_SHORT).show();
} else {
// At least one denied permission with ask never again
// Need to go to the settings
Toast.makeText(getView().getApplicationContext(), "Need to go to the settings", Toast.LENGTH_SHORT).show();
}
}
}
static class WeakRefSimpleCallback extends WeakRefPermissionSimpleCallback<MainActivity> {
public WeakRefSimpleCallback(MainActivity view) {
super(view);
}
@Override
public void onGranted() {
if (isFinish()) {
return;
}
// All permissions are granted !
Toast.makeText(getView().getApplicationContext(), "All permissions are granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeny() {
if (isFinish()) {
return;
}
// At least one denied permission
// Need to go to the settings
Toast.makeText(getView().getApplicationContext(), "Need to go to the settings", Toast.LENGTH_SHORT).show();
}
}
}