blob: 706a9c4c4a8ce4ff6aa84faba7732cb441bfa54d [file] [log] [blame]
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.cts.deviceowner;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory;
/**
* Simple activity that adds or clears a user restriction depending on the value of the extras.
*/
public final class SetPolicyActivity extends Activity {
private static final String TAG = SetPolicyActivity.class.getSimpleName();
private static final String EXTRA_RESTRICTION_KEY = "extra-restriction-key";
private static final String EXTRA_COMMAND = "extra-command";
private static final String ADD_RESTRICTION_COMMAND = "add-restriction";
private static final String CLEAR_RESTRICTION_COMMAND = "clear-restriction";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
// Overriding this method in case another intent is sent to this activity before finish()
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
DevicePolicyManager dpm = TestAppSystemServiceFactory.getDevicePolicyManager(this,
BasicAdminReceiver.class);
String command = intent.getStringExtra(EXTRA_COMMAND);
Log.i(TAG, "Command: \"" + command + " DPM: " + dpm);
ComponentName admin = BasicAdminReceiver.getComponentName(this);
if (ADD_RESTRICTION_COMMAND.equals(command)) {
String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
dpm.addUserRestriction(admin, restrictionKey);
Log.i(TAG, "Added user restriction " + restrictionKey
+ " for user " + Process.myUserHandle());
} else if (CLEAR_RESTRICTION_COMMAND.equals(command)) {
String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION_KEY);
dpm.clearUserRestriction(admin, restrictionKey);
Log.i(TAG, "Cleared user restriction " + restrictionKey
+ " for user " + Process.myUserHandle());
} else {
Log.e(TAG, "Invalid command: " + command);
}
}
}