Skip to content

Commit f90bc5d

Browse files
committed
Add lint checking to TestDPC (Attempt 2)
1. Lint is running in all build variants. Build will be aborted if any error is found. It should not affect build time much given that TestDPC is a small project. 2. We now only check the use of new API, and hopes that it can help identify the backward compatibility issue. 3. Move code of big switch statement into separate functions, so that we can add annotation separately. 4. Removed the getContext workaround added before as lint should able to catch this now. Bug: 27522307 Change-Id: I9c5903794e8592ca06986fee60ad4357721d70c6
1 parent 0ec73c5 commit f90bc5d

21 files changed

+194
-68
lines changed

app/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ android {
3131
}
3232
}
3333

34+
lintOptions {
35+
check 'NewApi'
36+
abortOnError true
37+
xmlReport false
38+
textReport true
39+
textOutput "stdout"
40+
}
41+
3442
signingConfigs {
3543
debug {
3644
storeFile file("$projectDir/debug.keystore")
@@ -79,6 +87,14 @@ android {
7987
}
8088
}
8189
}
90+
91+
// Enable lint checking in all build variants.
92+
applicationVariants.all { variant ->
93+
variant.outputs.each { output ->
94+
def lintTask = tasks["lint${variant.name.capitalize()}"]
95+
output.assemble.dependsOn lintTask
96+
}
97+
}
8298
}
8399

84100
dependencies {

app/src/main/java/com/afwsamples/testdpc/common/ProfileOrParentFragment.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.afwsamples.testdpc.common;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.Fragment;
2021
import android.app.admin.DevicePolicyManager;
2122
import android.content.ComponentName;
@@ -42,7 +43,10 @@
4243
*
4344
* If there is no parent user (for example, if the managed user is controlled by a Device Owner),
4445
* the fragment will be shown directly.
46+
*
47+
* Please notice that all subclasses of this fragment only support N or above.
4548
*/
49+
@TargetApi(VERSION_CODES.N)
4650
public abstract class ProfileOrParentFragment extends PreferenceFragment {
4751
private static final String LOG_TAG = "ProfileOrParentFragment";
4852

@@ -141,11 +145,6 @@ protected final boolean isManagedProfileInstance() {
141145
return mProfileOwner && !isParentProfileInstance();
142146
}
143147

144-
@Override
145-
public Context getContext() {
146-
return (Context) getActivity();
147-
}
148-
149148
@Override
150149
public void onCreate(Bundle savedInstanceState) {
151150
super.onCreate(savedInstanceState);

app/src/main/java/com/afwsamples/testdpc/common/ProvisioningStateUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.afwsamples.testdpc.common;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.admin.DevicePolicyManager;
2021
import android.content.ComponentName;
2122
import android.content.ContentResolver;
@@ -90,6 +91,7 @@ public static boolean isManaged(Context context) {
9091
* @param context Calling activity's context
9192
* @return true, if provisioning is allowed for corresponding action
9293
*/
94+
@TargetApi(Build.VERSION_CODES.N)
9395
public static boolean isProvisioningAllowed(Context context, String action) {
9496
/* TODO: Remove CODENAME check once SDK_INT on device is bumped for N */
9597
if (!versionIsAtLeastN()) {

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
2020

2121
import android.accessibilityservice.AccessibilityServiceInfo;
22+
import android.annotation.SuppressLint;
23+
import android.annotation.TargetApi;
2224
import android.app.Activity;
2325
import android.app.AlertDialog;
2426
import android.app.Fragment;
@@ -35,6 +37,7 @@
3537
import android.net.Uri;
3638
import android.os.AsyncTask;
3739
import android.os.BatteryManager;
40+
import android.os.Build;
3841
import android.os.Bundle;
3942
import android.os.UserHandle;
4043
import android.os.UserManager;
@@ -500,14 +503,7 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
500503
showRemoveDeviceOwnerPrompt();
501504
return true;
502505
case REQUEST_BUGREPORT_KEY:
503-
boolean startedSuccessfully = mDevicePolicyManager.requestBugreport(
504-
mAdminComponentName);
505-
if (!startedSuccessfully) {
506-
Context context = getContext();
507-
Util.showNotification(context, R.string.bugreport_title,
508-
context.getString(R.string.bugreport_failure_throttled),
509-
Util.BUGREPORT_NOTIFICATION_ID);
510-
}
506+
requestBugReport();
511507
return true;
512508
case REQUEST_PROCESS_LOGS:
513509
showFragment(new ProcessLogsFragment());
@@ -619,21 +615,16 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
619615
showFragment(new DelegatedCertInstallerFragment());
620616
return true;
621617
case DISABLE_STATUS_BAR:
622-
if (!mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, true)) {
623-
showToast("Unable to disable status bar when lock password is set.");
624-
}
618+
setStatusBarDisabled(true);
625619
return true;
626620
case REENABLE_STATUS_BAR:
627-
mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, false);
621+
setStatusBarDisabled(false);
628622
return true;
629623
case DISABLE_KEYGUARD:
630-
if (!mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, true)) {
631-
// this should not happen
632-
showToast("Unable to disable keyguard");
633-
}
624+
setKeyGuardDisabled(true);
634625
return true;
635626
case REENABLE_KEYGUARD:
636-
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, false);
627+
setKeyGuardDisabled(false);
637628
return true;
638629
case START_KIOSK_MODE:
639630
showManageLockTaskListPrompt(R.string.kiosk_select_title,
@@ -681,6 +672,7 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
681672
}
682673

683674
@Override
675+
@SuppressLint("NewApi")
684676
public boolean onPreferenceChange(Preference preference, Object newValue) {
685677
String key = preference.getKey();
686678

@@ -689,18 +681,16 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
689681
preference.setSummary((String) newValue);
690682
return true;
691683
case DISABLE_CAMERA_KEY:
692-
mDevicePolicyManager.setCameraDisabled(mAdminComponentName, (Boolean) newValue);
684+
setCameraDisabled((Boolean) newValue);
693685
// Reload UI to verify the camera is enable / disable correctly.
694686
reloadCameraDisableUi();
695687
return true;
696688
case ENABLE_PROCESS_LOGGING:
697-
mDevicePolicyManager.setSecurityLoggingEnabled(mAdminComponentName,
698-
(Boolean) newValue);
689+
setSecurityLoggingEnabled((Boolean) newValue);
699690
reloadEnableProcessLoggingUi();
700691
return true;
701692
case DISABLE_SCREEN_CAPTURE_KEY:
702-
mDevicePolicyManager.setScreenCaptureDisabled(mAdminComponentName,
703-
(Boolean) newValue);
693+
setScreenCaptureDisabled((Boolean) newValue);
704694
// Reload UI to verify that screen capture was enabled / disabled correctly.
705695
reloadScreenCaptureDisableUi();
706696
return true;
@@ -736,6 +726,58 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
736726
return false;
737727
}
738728

729+
@TargetApi(Build.VERSION_CODES.M)
730+
private void setCameraDisabled(boolean disabled) {
731+
mDevicePolicyManager.setCameraDisabled(mAdminComponentName, disabled);
732+
}
733+
734+
@TargetApi(Build.VERSION_CODES.N)
735+
private void setSecurityLoggingEnabled(boolean enabled) {
736+
mDevicePolicyManager.setSecurityLoggingEnabled(mAdminComponentName, enabled);
737+
}
738+
739+
@TargetApi(Build.VERSION_CODES.M)
740+
private void setKeyGuardDisabled(boolean disabled) {
741+
if (!mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, disabled)) {
742+
// this should not happen
743+
if (disabled) {
744+
showToast(R.string.unable_disable_keyguard);
745+
} else {
746+
showToast(R.string.unable_enable_keyguard);
747+
}
748+
}
749+
}
750+
751+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
752+
private void setScreenCaptureDisabled(boolean disabled) {
753+
mDevicePolicyManager.setScreenCaptureDisabled(mAdminComponentName, disabled);
754+
}
755+
756+
private void setMasterVolumeMuted(boolean muted) {
757+
758+
}
759+
760+
@TargetApi(Build.VERSION_CODES.N)
761+
private void requestBugReport() {
762+
boolean startedSuccessfully = mDevicePolicyManager.requestBugreport(
763+
mAdminComponentName);
764+
if (!startedSuccessfully) {
765+
Context context = getActivity();
766+
Util.showNotification(context, R.string.bugreport_title,
767+
context.getString(R.string.bugreport_failure_throttled),
768+
Util.BUGREPORT_NOTIFICATION_ID);
769+
}
770+
}
771+
772+
@TargetApi(Build.VERSION_CODES.M)
773+
private void setStatusBarDisabled(boolean disable) {
774+
if (!mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, disable)) {
775+
if (disable) {
776+
showToast("Unable to disable status bar when lock password is set.");
777+
}
778+
}
779+
}
780+
739781
/**
740782
* Dispatches an intent to capture image or video.
741783
*/
@@ -956,6 +998,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
956998
/**
957999
* Shows a message box with the device wifi mac address.
9581000
*/
1001+
@TargetApi(Build.VERSION_CODES.N)
9591002
private void showWifiMacAddress() {
9601003
final String macAddress = mDevicePolicyManager.getWifiMacAddress(mAdminComponentName);
9611004
final String message = macAddress != null ? macAddress
@@ -1068,6 +1111,7 @@ private void disableIncompatibleManagementOptionsByApiLevel() {
10681111
* Shows the default response for future runtime permission requests by applications, and lets
10691112
* the user change the default value.
10701113
*/
1114+
@TargetApi(Build.VERSION_CODES.M)
10711115
private void showSetPermissionPolicyDialog() {
10721116
if (getActivity() == null || getActivity().isFinishing()) {
10731117
return;
@@ -1190,6 +1234,7 @@ private void showDisableAccountTypeList() {
11901234
* Shows a prompt asking for the username of the new user and whether the setup wizard should
11911235
* be skipped.
11921236
*/
1237+
@TargetApi(Build.VERSION_CODES.N)
11931238
private void showCreateAndManageUserPrompt() {
11941239
if (getActivity() == null || getActivity().isFinishing()) {
11951240
return;
@@ -1317,11 +1362,13 @@ public void onClick(DialogInterface dialogInterface, int i) {
13171362
.show();
13181363
}
13191364

1365+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
13201366
private void reloadCameraDisableUi() {
13211367
boolean isCameraDisabled = mDevicePolicyManager.getCameraDisabled(mAdminComponentName);
13221368
mDisableCameraSwitchPreference.setChecked(isCameraDisabled);
13231369
}
13241370

1371+
@TargetApi(Build.VERSION_CODES.N)
13251372
private void reloadEnableProcessLoggingUi() {
13261373
if (mEnableProcessLoggingPreference.isEnabled()) {
13271374
boolean isProcessLoggingEnabled = mDevicePolicyManager.isSecurityLoggingEnabled(
@@ -1331,19 +1378,22 @@ private void reloadEnableProcessLoggingUi() {
13311378
}
13321379
}
13331380

1381+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
13341382
private void reloadScreenCaptureDisableUi() {
13351383
boolean isScreenCaptureDisabled = mDevicePolicyManager.getScreenCaptureDisabled(
13361384
mAdminComponentName);
13371385
mDisableScreenCaptureSwitchPreference.setChecked(isScreenCaptureDisabled);
13381386
}
13391387

1388+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
13401389
private void reloadSetAutoTimeRequiredUi() {
13411390
if (mDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
13421391
boolean isAutoTimeRequired = mDevicePolicyManager.getAutoTimeRequired();
13431392
mSetAutoTimeRequiredPreference.setChecked(isAutoTimeRequired);
13441393
}
13451394
}
13461395

1396+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
13471397
private void reloadMuteAudioUi() {
13481398
final boolean isAudioMuted = mDevicePolicyManager.isMasterVolumeMuted(mAdminComponentName);
13491399
mMuteAudioSwitchPreference.setChecked(isAudioMuted);
@@ -1542,6 +1592,7 @@ public void onClick(DialogInterface dialog, int which) {
15421592
*
15431593
* Once the alias is chosen and deleted, a {@link Toast} shows status- success or failure.
15441594
*/
1595+
@TargetApi(Build.VERSION_CODES.N)
15451596
private void choosePrivateKeyForRemoval() {
15461597
KeyChain.choosePrivateKeyAlias(getActivity(), new KeyChainAliasCallback() {
15471598
@Override
@@ -1795,6 +1846,7 @@ public void onClick(DialogInterface dialog, int position) {
17951846
/**
17961847
* Shows an alert dialog which displays a list of suspended/non-suspended apps.
17971848
*/
1849+
@TargetApi(Build.VERSION_CODES.N)
17981850
private void showSuspendAppsPrompt(final boolean forUnsuspending) {
17991851
final List<String> showApps = new ArrayList<>();
18001852
if (forUnsuspending) {
@@ -2063,6 +2115,7 @@ private void showWifiConfigCreationDialog() {
20632115
dialog.show(getFragmentManager(), TAG_WIFI_CONFIG_CREATION);
20642116
}
20652117

2118+
@TargetApi(Build.VERSION_CODES.N)
20662119
private void reboot() {
20672120
if (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
20682121
showToast(R.string.reboot_error_msg);
@@ -2071,12 +2124,7 @@ private void reboot() {
20712124
mDevicePolicyManager.reboot(mAdminComponentName);
20722125
}
20732126

2074-
abstract class ManageLockTaskListCallback {
2127+
abstract class ManageLockTaskListCallback {
20752128
public abstract void onPositiveButtonClicked(String[] lockTaskArray);
20762129
}
2077-
2078-
@Override
2079-
public Context getContext() {
2080-
return (Context) getActivity();
2081-
}
20822130
}

app/src/main/java/com/afwsamples/testdpc/policy/ProcessLogsFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.afwsamples.testdpc.policy;
22

3+
import android.annotation.TargetApi;
34
import android.app.ListFragment;
45
import android.app.admin.DevicePolicyManager;
56
import android.app.admin.SecurityLog;
67
import android.app.admin.SecurityLog.SecurityEvent;
78
import android.content.ComponentName;
89
import android.content.Context;
10+
import android.os.Build;
911
import android.os.Bundle;
1012
import android.util.Log;
1113
import android.widget.ArrayAdapter;
@@ -20,6 +22,7 @@
2022
import java.util.List;
2123
import java.util.concurrent.TimeUnit;
2224

25+
@TargetApi(Build.VERSION_CODES.N)
2326
public class ProcessLogsFragment extends ListFragment {
2427

2528
private static final String TAG = "ProcessLogsFragment";

app/src/main/java/com/afwsamples/testdpc/policy/SetSupportMessageFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.afwsamples.testdpc.policy;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.admin.DevicePolicyManager;
2021
import android.app.Fragment;
2122
import android.content.ComponentName;
2223
import android.content.Context;
24+
import android.os.Build;
2325
import android.os.Bundle;
2426
import android.view.LayoutInflater;
2527
import android.view.View;
@@ -29,6 +31,7 @@
2931
import com.afwsamples.testdpc.DeviceAdminReceiver;
3032
import com.afwsamples.testdpc.R;
3133

34+
@TargetApi(Build.VERSION_CODES.N)
3235
public class SetSupportMessageFragment extends Fragment
3336
implements View.OnClickListener {
3437
private static final String ARG_MESSAGE_TYPE = "message_type";

app/src/main/java/com/afwsamples/testdpc/policy/certificate/DelegatedCertInstallerFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package com.afwsamples.testdpc.policy.certificate;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.admin.DevicePolicyManager;
2021
import android.content.Context;
22+
import android.os.Build;
2123
import android.os.Bundle;
2224

2325
import com.afwsamples.testdpc.DeviceAdminReceiver;
@@ -30,6 +32,7 @@
3032
* 1) {@link DevicePolicyManager#setCertInstallerPackage}
3133
* 2) {@link DevicePolicyManager#getCertInstallerPackage}
3234
*/
35+
@TargetApi(Build.VERSION_CODES.M)
3336
public class DelegatedCertInstallerFragment extends SelectAppFragment {
3437

3538
private DevicePolicyManager mDpm;

0 commit comments

Comments
 (0)