Skip to content

Commit c42486d

Browse files
committed
v6.02: Fixed a problem with suggesting IMEI and serial number as device IDs at first start
1 parent 811a30c commit c42486d

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ android {
3838
// To avoid extra step during initial setup, let's keep the target SDK version as 29 as long as possible
3939
//noinspection ExpiredTargetSdkVersion
4040
targetSdkVersion 34
41-
versionCode 15010
42-
versionName "6.01"
41+
versionCode 15020
42+
versionName "6.02"
4343
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4444
dataBinding {
4545
enabled = true

app/src/main/java/com/hmdm/launcher/ui/BaseActivity.java

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ protected void createAndShowEnterDeviceIdDialog( boolean error, String deviceId
114114
// if it's bound to IMEI, it becomes difficult to replace the device
115115
List<String> variantsList = new ArrayList<>();
116116
if (!BuildConfig.DEVICE_ID_CHOICE.equals("user")) {
117+
Utils.autoGrantPhonePermission(this);
117118
String imei = DeviceInfoProvider.getImei(this);
118119
if (imei != null) {
119120
variantsList.add(imei);

app/src/main/java/com/hmdm/launcher/ui/MainActivity.java

+1
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ private void startLauncher() {
962962
createAndShowServerDialog(false, settingsHelper.getBaseUrl(), settingsHelper.getServerProject());
963963
} else if ( settingsHelper.getDeviceId().length() == 0 ) {
964964
Log.d(Const.LOG_TAG, "Device ID is empty");
965+
Utils.autoGrantPhonePermission(this);
965966
if (!SystemUtils.autoSetDeviceId(this)) {
966967
createAndShowEnterDeviceIdDialog(false, null);
967968
} else {

app/src/main/java/com/hmdm/launcher/ui/MdmChoiceSetupActivity.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hmdm.launcher.ui;
22

3+
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MODE;
4+
35
import android.app.Dialog;
46
import android.app.admin.DevicePolicyManager;
57
import android.content.Intent;
@@ -24,13 +26,10 @@
2426
import com.hmdm.launcher.databinding.ActivityMdmChoiceBinding;
2527
import com.hmdm.launcher.databinding.DialogEnterDeviceIdBinding;
2628
import com.hmdm.launcher.helper.SettingsHelper;
27-
import com.hmdm.launcher.util.DeviceInfoProvider;
2829

2930
import java.util.ArrayList;
3031
import java.util.List;
3132

32-
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MODE;
33-
3433
public class MdmChoiceSetupActivity extends AppCompatActivity {
3534
private ActivityMdmChoiceBinding binding;
3635

@@ -71,7 +70,8 @@ protected void onCreate( Bundle savedInstanceState ) {
7170
} else if (BuildConfig.DEVICE_ID_CHOICE.equals("serial") || "serial".equals(deviceIdUse)) {
7271
deviceId = intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_SERIAL_NUMBER);
7372
} else {
74-
displayEnterDeviceIdDialog();
73+
displayEnterDeviceIdDialog(intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_IMEI),
74+
intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_SERIAL_NUMBER));
7575
return;
7676
}
7777
settingsHelper.setDeviceId(deviceId);
@@ -91,7 +91,7 @@ public void continueSetup(View view) {
9191
finish();
9292
}
9393

94-
protected void displayEnterDeviceIdDialog() {
94+
protected void displayEnterDeviceIdDialog(String imei, String serial) {
9595
enterDeviceIdDialog = new Dialog(this);
9696
enterDeviceIdDialogBinding = DataBindingUtil.inflate(
9797
LayoutInflater.from( this ),
@@ -112,11 +112,9 @@ protected void displayEnterDeviceIdDialog() {
112112

113113
// Suggest variants to choose the device ID: IMEI or serial
114114
List<String> variantsList = new ArrayList<>();
115-
String imei = DeviceInfoProvider.getImei(this);
116115
if (imei != null) {
117116
variantsList.add(imei);
118117
}
119-
String serial = DeviceInfoProvider.getSerialNumber();
120118
if (serial != null && !serial.equals(Build.UNKNOWN)) {
121119
variantsList.add(serial);
122120
}

app/src/main/java/com/hmdm/launcher/util/Utils.java

+29
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,35 @@ public static String getLauncherVariant() {
7777
return BuildConfig.FLAVOR == null || BuildConfig.FLAVOR.equals("") ? "opensource" : BuildConfig.FLAVOR;
7878
}
7979

80+
// Automatically grant permission to get phone state (for IMEI and serial)
81+
@TargetApi(Build.VERSION_CODES.M)
82+
public static boolean autoGrantPhonePermission(Context context) {
83+
try {
84+
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
85+
Context.DEVICE_POLICY_SERVICE);
86+
ComponentName adminComponentName = LegacyUtils.getAdminComponentName(context);
87+
88+
if (devicePolicyManager.getPermissionGrantState(adminComponentName,
89+
context.getPackageName(), Manifest.permission.READ_PHONE_STATE) != DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED) {
90+
boolean success = devicePolicyManager.setPermissionGrantState(adminComponentName,
91+
context.getPackageName(), Manifest.permission.READ_PHONE_STATE, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
92+
if (!success) {
93+
return false;
94+
}
95+
}
96+
} catch (NoSuchMethodError e) {
97+
// This exception is raised on Android 5.1
98+
e.printStackTrace();
99+
return false;
100+
} catch (/* SecurityException */ Exception e) {
101+
// No active admin ComponentInfo (not sure why could that happen)
102+
e.printStackTrace();
103+
return false;
104+
}
105+
Log.i(Const.LOG_TAG, "READ_PHONE_STATE automatically granted");
106+
return true;
107+
}
108+
80109
// Automatically get dangerous permissions
81110
// Notice: default (null) app permission strategy is "Grant all"
82111
@TargetApi(Build.VERSION_CODES.M)

0 commit comments

Comments
 (0)