|
1 | 1 | package rikka.smscodehelper;
|
2 | 2 |
|
3 | 3 | import android.Manifest;
|
4 |
| -import android.content.DialogInterface; |
| 4 | +import android.app.Activity; |
| 5 | +import android.app.AlertDialog; |
5 | 6 | import android.content.pm.PackageManager;
|
6 | 7 | import android.os.Build;
|
7 | 8 | import android.os.Bundle;
|
8 |
| -import android.support.v4.app.ActivityCompat; |
9 |
| -import android.support.v4.content.ContextCompat; |
10 |
| -import android.support.v7.app.AlertDialog; |
11 |
| -import android.support.v7.app.AppCompatActivity; |
12 | 9 |
|
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.core.app.ActivityCompat; |
| 12 | +import androidx.core.content.ContextCompat; |
13 | 13 |
|
14 |
| -public class MainActivity extends AppCompatActivity { |
| 14 | +public class MainActivity extends Activity { |
15 | 15 |
|
16 | 16 | @Override
|
17 | 17 | protected void onCreate(Bundle savedInstanceState) {
|
18 | 18 | super.onCreate(savedInstanceState);
|
19 |
| - setContentView(R.layout.activity_main); |
20 | 19 |
|
21 |
| - AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle); |
| 20 | + AlertDialog.Builder builder = new AlertDialog.Builder(this) |
| 21 | + .setOnCancelListener(dialog -> finish()) |
| 22 | + .setCancelable(false); |
22 | 23 |
|
23 |
| - builder.setOnCancelListener(new DialogInterface.OnCancelListener() { |
24 |
| - @Override |
25 |
| - public void onCancel(DialogInterface dialog) { |
26 |
| - finish(); |
27 |
| - } |
28 |
| - }); |
29 |
| - |
30 |
| - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && |
31 |
| - ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { |
| 24 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M |
| 25 | + && ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { |
32 | 26 | builder.setMessage(R.string.tip_m);
|
33 |
| - builder.setPositiveButton(R.string.get_permission, new DialogInterface.OnClickListener() { |
34 |
| - @Override |
35 |
| - public void onClick(DialogInterface dialog, int which) { |
36 |
| - getPermission(Manifest.permission.RECEIVE_SMS); |
37 |
| - } |
38 |
| - }); |
| 27 | + builder.setPositiveButton(R.string.get_permission, (dialog, which) -> requestPermission()); |
| 28 | + builder.setNegativeButton(R.string.exit, (dialog, which) -> finish()); |
39 | 29 | } else {
|
40 | 30 | builder.setMessage(getString(R.string.tip));
|
41 |
| - builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
42 |
| - @Override |
43 |
| - public void onClick(DialogInterface dialog, int which) { |
44 |
| - hideLauncher(); |
45 |
| - finish(); |
46 |
| - } |
| 31 | + builder.setPositiveButton(android.R.string.ok, (dialog, which) -> { |
| 32 | + hideFromLauncher(); |
| 33 | + finish(); |
47 | 34 | });
|
48 | 35 | }
|
49 | 36 |
|
50 |
| - AlertDialog alertDialog = builder.create(); |
51 |
| - alertDialog.setCanceledOnTouchOutside(false); |
52 |
| - alertDialog.show(); |
| 37 | + builder.show(); |
53 | 38 | }
|
54 | 39 |
|
55 |
| - private void getPermission(String permission) { |
56 |
| - if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { |
57 |
| - ActivityCompat.requestPermissions(this, new String[]{permission}, 0); |
| 40 | + private void requestPermission() { |
| 41 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { |
| 42 | + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 0); |
58 | 43 | }
|
59 | 44 | }
|
60 | 45 |
|
61 | 46 | @Override
|
62 |
| - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
| 47 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
63 | 48 | switch (requestCode) {
|
64 | 49 | case 0:
|
65 | 50 | if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
|
66 |
| - AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle); |
| 51 | + AlertDialog.Builder builder = new AlertDialog.Builder(this); |
67 | 52 |
|
68 |
| - builder.setTitle("OAQ"); |
69 | 53 | builder.setMessage(R.string.permission_denied);
|
70 |
| - builder.setPositiveButton(R.string.exit, new DialogInterface.OnClickListener() { |
71 |
| - @Override |
72 |
| - public void onClick(DialogInterface dialog, int which) { |
73 |
| - finish(); |
74 |
| - } |
75 |
| - }); |
76 |
| - builder.create().show(); |
| 54 | + builder.setPositiveButton(R.string.exit, (dialog, which) -> finish()); |
| 55 | + builder.show(); |
77 | 56 | } else {
|
| 57 | + hideFromLauncher(); |
78 | 58 | finish();
|
79 |
| - hideLauncher(); |
80 | 59 | }
|
81 | 60 | break;
|
82 | 61 | default:
|
83 | 62 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
84 | 63 | }
|
85 | 64 | }
|
86 | 65 |
|
87 |
| - private void hideLauncher() { |
88 |
| - PackageManager p = getApplicationContext().getPackageManager(); |
89 |
| - p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
| 66 | + private void hideFromLauncher() { |
| 67 | + PackageManager pm = getApplicationContext().getPackageManager(); |
| 68 | + pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
90 | 69 | }
|
91 | 70 | }
|
0 commit comments