Skip to content

Commit a908bf7

Browse files
authored
Merge pull request #486 from Microsoft/fix/unknown_sources_android_o
Can't check unknown sources detection on Android 8 if target is below 26
2 parents 90437d8 + 1d5bd9c commit a908bf7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

sdk/mobile-center-distribute/src/main/java/com/microsoft/azure/mobile/distribute/InstallerUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.microsoft.azure.mobile.distribute;
22

3-
import android.content.ContentResolver;
43
import android.content.Context;
54
import android.os.Build;
65
import android.provider.Settings;
@@ -70,8 +69,15 @@ static synchronized boolean isInstalledFromAppStore(@NonNull String logTag, @Non
7069
*/
7170
@SuppressWarnings("deprecation")
7271
static boolean isUnknownSourcesEnabled(@NonNull Context context) {
72+
73+
/*
74+
* On Android 8 with applications targeting lower versions,
75+
* it's impossible to check unknown sources enabled: using old APIs will always return true
76+
* and using the new one will always return false,
77+
* so in order to avoid a stuck dialog that can't be bypassed we will assume true.
78+
*/
7379
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
74-
return context.getPackageManager().canRequestPackageInstalls();
80+
return context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O || context.getPackageManager().canRequestPackageInstalls();
7581
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
7682
return INSTALL_NON_MARKET_APPS_ENABLED.equals(Settings.Global.getString(context.getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS));
7783
} else {

sdk/mobile-center-distribute/src/test/java/com/microsoft/azure/mobile/distribute/UnknownSourcesDetectionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.ContentResolver;
55
import android.content.Context;
6+
import android.content.pm.ApplicationInfo;
67
import android.content.pm.PackageManager;
78
import android.os.Build;
89
import android.provider.Settings;
@@ -40,6 +41,9 @@ public class UnknownSourcesDetectionTest {
4041
@Mock
4142
private PackageManager mPackageManager;
4243

44+
@Mock
45+
private ApplicationInfo mApplicationInfo;
46+
4347
private static void mockApiLevel(int apiLevel) {
4448
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", apiLevel);
4549
}
@@ -49,6 +53,7 @@ public void setUp() {
4953
mockStatic(Settings.Global.class);
5054
mockStatic(Settings.Secure.class);
5155
when(mContext.getPackageManager()).thenReturn(mPackageManager);
56+
when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
5257
}
5358

5459
@Test
@@ -71,12 +76,23 @@ public void unknownSourcesEnabledViaPackageManager() {
7176
assertFalse(InstallerUtils.isUnknownSourcesEnabled(mContext));
7277
verify(mPackageManager, never()).canRequestPackageInstalls();
7378
}
79+
80+
/* Test from Android 8 targeting that Android version. */
7481
for (int apiLevel = Build.VERSION_CODES.O; apiLevel <= BuildConfig.TARGET_SDK_VERSION; apiLevel++) {
7582
mockApiLevel(apiLevel);
83+
Whitebox.setInternalState(mApplicationInfo, "targetSdkVersion", apiLevel);
7684
assertTrue(InstallerUtils.isUnknownSourcesEnabled(mContext));
7785
verify(mPackageManager).canRequestPackageInstalls();
7886
reset(mPackageManager);
7987
}
88+
89+
/* Test from Android 8 targeting older versions: always true. */
90+
Whitebox.setInternalState(mApplicationInfo, "targetSdkVersion", Build.VERSION_CODES.N_MR1);
91+
for (int apiLevel = Build.VERSION_CODES.O; apiLevel <= BuildConfig.TARGET_SDK_VERSION; apiLevel++) {
92+
mockApiLevel(apiLevel);
93+
assertTrue(InstallerUtils.isUnknownSourcesEnabled(mContext));
94+
verify(mPackageManager, never()).canRequestPackageInstalls();
95+
}
8096
}
8197

8298
@Test
@@ -101,6 +117,7 @@ public void unknownSourcesEnabledViaSystemSecure() {
101117
}
102118
for (int apiLevel = Build.VERSION_CODES.O; apiLevel <= BuildConfig.TARGET_SDK_VERSION; apiLevel++) {
103119
mockApiLevel(apiLevel);
120+
Whitebox.setInternalState(mApplicationInfo, "targetSdkVersion", apiLevel);
104121
assertFalse(InstallerUtils.isUnknownSourcesEnabled(mContext));
105122
verify(mPackageManager).canRequestPackageInstalls();
106123
reset(mPackageManager);
@@ -129,6 +146,7 @@ public void unknownSourcesEnabledViaSystemGlobal() {
129146
}
130147
for (int apiLevel = Build.VERSION_CODES.O; apiLevel <= BuildConfig.TARGET_SDK_VERSION; apiLevel++) {
131148
mockApiLevel(apiLevel);
149+
Whitebox.setInternalState(mApplicationInfo, "targetSdkVersion", apiLevel);
132150
assertFalse(InstallerUtils.isUnknownSourcesEnabled(mContext));
133151
verify(mPackageManager).canRequestPackageInstalls();
134152
reset(mPackageManager);

0 commit comments

Comments
 (0)