|
1 | 1 | package org.thehellnet.mobile.myinfos.activity; |
2 | 2 |
|
| 3 | +import android.Manifest; |
| 4 | +import android.app.Activity; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.pm.PackageManager; |
3 | 7 | import android.os.Bundle; |
4 | | -import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.support.annotation.NonNull; |
| 9 | +import android.support.v4.app.ActivityCompat; |
| 10 | +import android.support.v4.content.ContextCompat; |
5 | 11 | import android.telephony.TelephonyManager; |
6 | 12 | import android.view.Menu; |
7 | 13 | import android.view.MenuItem; |
|
13 | 19 | import org.thehellnet.mobile.myinfos.R; |
14 | 20 | import org.thehellnet.mobile.myinfos.utility.AppUtils; |
15 | 21 |
|
16 | | -public class MainActivity extends AppCompatActivity { |
| 22 | +public class MainActivity extends Activity { |
| 23 | + |
| 24 | + private static final int REQUEST_CODE_READ_PHONE_STATE = 1; |
17 | 25 |
|
18 | 26 | private TelephonyManager telephonyManager; |
19 | 27 |
|
@@ -46,32 +54,89 @@ public boolean onOptionsItemSelected(MenuItem item) { |
46 | 54 | @Override |
47 | 55 | protected void onResume() { |
48 | 56 | super.onResume(); |
49 | | - updateUiValues(); |
| 57 | + |
| 58 | + if (checkPermissions()) { |
| 59 | + updateUiValues(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onRequestPermissionsResult(int requestCode, |
| 65 | + @NonNull String[] permissions, |
| 66 | + @NonNull int[] grantResults) { |
| 67 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 68 | + |
| 69 | + if (grantResults.length > 0) { |
| 70 | + switch (requestCode) { |
| 71 | + case REQUEST_CODE_READ_PHONE_STATE: |
| 72 | + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 73 | + updateUiValues(); |
| 74 | + } else { |
| 75 | + startActivity(new Intent(MyInfos.getAppContext(), NoPermsActivity.class)); |
| 76 | + finish(); |
| 77 | + } |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
50 | 81 | } |
51 | 82 |
|
52 | 83 | private void initPrivates() { |
53 | 84 | telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); |
54 | 85 | } |
55 | 86 |
|
| 87 | + private boolean checkPermissions() { |
| 88 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) |
| 89 | + == PackageManager.PERMISSION_GRANTED) { |
| 90 | + return true; |
| 91 | + } else { |
| 92 | + ActivityCompat.requestPermissions(this, |
| 93 | + new String[]{Manifest.permission.READ_PHONE_STATE}, |
| 94 | + REQUEST_CODE_READ_PHONE_STATE); |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
56 | 99 | private void updateUiVersion() { |
57 | 100 | TextView version = (TextView) findViewById(R.id.version_value); |
58 | 101 | version.setText(String.format("App version %s", AppUtils.getAppVersion())); |
59 | 102 | } |
60 | 103 |
|
61 | 104 | private void updateUiValues() { |
62 | | - EditText imei = (EditText) findViewById(R.id.imei_value); |
63 | | - imei.setText(telephonyManager.getDeviceId()); |
64 | | - |
65 | | - EditText iccid = (EditText) findViewById(R.id.iccid_value); |
66 | | - iccid.setText(telephonyManager.getSimSerialNumber()); |
67 | | - EditText number = (EditText) findViewById(R.id.number_value); |
68 | | - number.setText(telephonyManager.getLine1Number().length() > 0 |
69 | | - ? telephonyManager.getLine1Number() |
70 | | - : getString(R.string.ui_value_notdefined)); |
71 | | - EditText operator = (EditText) findViewById(R.id.operator_value); |
72 | | - operator.setText(telephonyManager.getNetworkOperatorName()); |
73 | | - EditText subscriber = (EditText) findViewById(R.id.subscriber_value); |
74 | | - subscriber.setText(telephonyManager.getSubscriberId()); |
| 105 | + if (telephonyManager == null) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (telephonyManager.getDeviceId() != null) { |
| 110 | + EditText imei = (EditText) findViewById(R.id.imei_value); |
| 111 | + imei.setText(telephonyManager.getDeviceId()); |
| 112 | + } |
| 113 | + |
| 114 | + if (telephonyManager.getDeviceSoftwareVersion() != null) { |
| 115 | + EditText swver = (EditText) findViewById(R.id.swver_value); |
| 116 | + swver.setText(telephonyManager.getDeviceSoftwareVersion()); |
| 117 | + } |
| 118 | + |
| 119 | + if (telephonyManager.getSimSerialNumber() != null) { |
| 120 | + EditText iccid = (EditText) findViewById(R.id.iccid_value); |
| 121 | + iccid.setText(telephonyManager.getSimSerialNumber()); |
| 122 | + } |
| 123 | + |
| 124 | + if (telephonyManager.getLine1Number() != null) { |
| 125 | + EditText number = (EditText) findViewById(R.id.number_value); |
| 126 | + number.setText(telephonyManager.getLine1Number().length() > 0 |
| 127 | + ? telephonyManager.getLine1Number() |
| 128 | + : getString(R.string.ui_value_notdefined)); |
| 129 | + } |
| 130 | + |
| 131 | + if (telephonyManager.getNetworkOperatorName() != null) { |
| 132 | + EditText operator = (EditText) findViewById(R.id.operator_value); |
| 133 | + operator.setText(telephonyManager.getNetworkOperatorName()); |
| 134 | + } |
| 135 | + |
| 136 | + if (telephonyManager.getSubscriberId() != null) { |
| 137 | + EditText subscriber = (EditText) findViewById(R.id.subscriber_value); |
| 138 | + subscriber.setText(telephonyManager.getSubscriberId()); |
| 139 | + } |
75 | 140 | } |
76 | 141 |
|
77 | 142 | private void showVersionToast() { |
|
0 commit comments