/*
 * Copyright (C) 2016 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.afwsamples.testdpc.profilepolicy.apprestrictions;

import android.annotation.TargetApi;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;

import com.afwsamples.testdpc.DeviceAdminReceiver;
import com.afwsamples.testdpc.R;
import com.afwsamples.testdpc.common.SelectAppFragment;

import java.lang.IllegalArgumentException;

/**
 * This fragment lets the user select an app that can manage application restrictions for the
 * current user. Related APIs:
 * 1) {@link DevicePolicyManager#setApplicationRestrictionsManagingPackage}
 * 2) {@link DevicePolicyManager#getApplicationRestrictionsManagingPackage}
 * 3) {@link DevicePolicyManager#isCallerApplicationRestrictionsManagingPackage}
 */
@TargetApi(Build.VERSION_CODES.N)
public class AppRestrictionsManagingPackageFragment extends SelectAppFragment {

    private DevicePolicyManager mDpm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDpm = (DevicePolicyManager) getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().getActionBar().setTitle(R.string.app_restrictions_managing_package);
    }

    @Override
    protected void setSelectedPackage(String pkgName) {
        try {
            mDpm.setApplicationRestrictionsManagingPackage(
                    DeviceAdminReceiver.getComponentName(getActivity()), pkgName);
        } catch (NameNotFoundException nnpe) {
            throw new IllegalArgumentException(nnpe);
        }
    }

    @Override
    protected void clearSelectedPackage() {
        setSelectedPackage(null);
    }

    @Override
    protected String getSelectedPackage() {
        return mDpm.getApplicationRestrictionsManagingPackage(
                DeviceAdminReceiver.getComponentName(getActivity()));
    }
}

@@ -906,16 +906,47 @@
 .Bundle;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -1084,52 +1084,8 @@
 t;%0A%0A
-import java.lang.IllegalArgumentException;%0A%0A
 /**%0A
@@ -2050,24 +2050,194 @@
  pkgName) %7B%0A
+        // If the input pkgName is an empty string, we clear the app restriction manager.%0A        if (TextUtils.isEmpty(pkgName)) %7B%0A            pkgName = null;%0A        %7D%0A
         try 

Clear the app restriction manager if the pkg name is empty string