Skip to content

Commit

Permalink
v6.02: Fixed a problem with suggesting IMEI and serial number as devi…
Browse files Browse the repository at this point in the history
…ce IDs at first start
  • Loading branch information
vmayorow committed Sep 11, 2024
1 parent 811a30c commit c42486d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ android {
// To avoid extra step during initial setup, let's keep the target SDK version as 29 as long as possible
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 34
versionCode 15010
versionName "6.01"
versionCode 15020
versionName "6.02"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dataBinding {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/hmdm/launcher/ui/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ protected void createAndShowEnterDeviceIdDialog( boolean error, String deviceId
// if it's bound to IMEI, it becomes difficult to replace the device
List<String> variantsList = new ArrayList<>();
if (!BuildConfig.DEVICE_ID_CHOICE.equals("user")) {
Utils.autoGrantPhonePermission(this);
String imei = DeviceInfoProvider.getImei(this);
if (imei != null) {
variantsList.add(imei);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/hmdm/launcher/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ private void startLauncher() {
createAndShowServerDialog(false, settingsHelper.getBaseUrl(), settingsHelper.getServerProject());
} else if ( settingsHelper.getDeviceId().length() == 0 ) {
Log.d(Const.LOG_TAG, "Device ID is empty");
Utils.autoGrantPhonePermission(this);
if (!SystemUtils.autoSetDeviceId(this)) {
createAndShowEnterDeviceIdDialog(false, null);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hmdm.launcher.ui;

import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MODE;

import android.app.Dialog;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
Expand All @@ -24,13 +26,10 @@
import com.hmdm.launcher.databinding.ActivityMdmChoiceBinding;
import com.hmdm.launcher.databinding.DialogEnterDeviceIdBinding;
import com.hmdm.launcher.helper.SettingsHelper;
import com.hmdm.launcher.util.DeviceInfoProvider;

import java.util.ArrayList;
import java.util.List;

import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MODE;

public class MdmChoiceSetupActivity extends AppCompatActivity {
private ActivityMdmChoiceBinding binding;

Expand Down Expand Up @@ -71,7 +70,8 @@ protected void onCreate( Bundle savedInstanceState ) {
} else if (BuildConfig.DEVICE_ID_CHOICE.equals("serial") || "serial".equals(deviceIdUse)) {
deviceId = intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_SERIAL_NUMBER);
} else {
displayEnterDeviceIdDialog();
displayEnterDeviceIdDialog(intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_IMEI),
intent.getStringExtra(DevicePolicyManager.EXTRA_PROVISIONING_SERIAL_NUMBER));
return;
}
settingsHelper.setDeviceId(deviceId);
Expand All @@ -91,7 +91,7 @@ public void continueSetup(View view) {
finish();
}

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

// Suggest variants to choose the device ID: IMEI or serial
List<String> variantsList = new ArrayList<>();
String imei = DeviceInfoProvider.getImei(this);
if (imei != null) {
variantsList.add(imei);
}
String serial = DeviceInfoProvider.getSerialNumber();
if (serial != null && !serial.equals(Build.UNKNOWN)) {
variantsList.add(serial);
}
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/com/hmdm/launcher/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ public static String getLauncherVariant() {
return BuildConfig.FLAVOR == null || BuildConfig.FLAVOR.equals("") ? "opensource" : BuildConfig.FLAVOR;
}

// Automatically grant permission to get phone state (for IMEI and serial)
@TargetApi(Build.VERSION_CODES.M)
public static boolean autoGrantPhonePermission(Context context) {
try {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
ComponentName adminComponentName = LegacyUtils.getAdminComponentName(context);

if (devicePolicyManager.getPermissionGrantState(adminComponentName,
context.getPackageName(), Manifest.permission.READ_PHONE_STATE) != DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED) {
boolean success = devicePolicyManager.setPermissionGrantState(adminComponentName,
context.getPackageName(), Manifest.permission.READ_PHONE_STATE, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
if (!success) {
return false;
}
}
} catch (NoSuchMethodError e) {
// This exception is raised on Android 5.1
e.printStackTrace();
return false;
} catch (/* SecurityException */ Exception e) {
// No active admin ComponentInfo (not sure why could that happen)
e.printStackTrace();
return false;
}
Log.i(Const.LOG_TAG, "READ_PHONE_STATE automatically granted");
return true;
}

// Automatically get dangerous permissions
// Notice: default (null) app permission strategy is "Grant all"
@TargetApi(Build.VERSION_CODES.M)
Expand Down

0 comments on commit c42486d

Please sign in to comment.