Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4cd2b33d authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Android (Google) Code Review
Browse files

Merge "Add padlock to usb modes when disabled by admin."

parents 450ebb58 1c3fd76e
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -134,13 +134,16 @@ public class UsbBackend {
                    ? UsbPort.POWER_ROLE_SOURCE : UsbPort.POWER_ROLE_SINK;
    }

    public boolean isModeSupported(int mode) {
    public boolean isModeDisallowedByAdmin(int mode) {
        if (mRestricted && (mode & MODE_DATA_MASK) != MODE_DATA_NONE
                && (mode & MODE_DATA_MASK) != MODE_DATA_MIDI) {
            // No USB data modes are supported.
            return true;
        }
        return false;
    }

    public boolean isModeSupported(int mode) {
        if (!mMidi && (mode & MODE_DATA_MASK) == MODE_DATA_MIDI) {
            return false;
        }
+43 −4
Original line number Diff line number Diff line
@@ -25,8 +25,12 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuff;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
@@ -35,6 +39,9 @@ import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtils;

import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

/**
 * UI for the USB chooser dialog.
@@ -53,6 +60,7 @@ public class UsbModeChooserActivity extends Activity {
    private UsbBackend mBackend;
    private AlertDialog mDialog;
    private LayoutInflater mLayoutInflater;
    private EnforcedAdmin mEnforcedAdmin;

    private BroadcastReceiver mDisconnectedReceiver = new BroadcastReceiver() {
        @Override
@@ -93,11 +101,14 @@ public class UsbModeChooserActivity extends Activity {

        LinearLayout container = (LinearLayout) mDialog.findViewById(R.id.container);

        mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(this,
                UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.myUserId());
        mBackend = new UsbBackend(this);
        int current = mBackend.getCurrentMode();
        for (int i = 0; i < DEFAULT_MODES.length; i++) {
            if (mBackend.isModeSupported(DEFAULT_MODES[i])) {
                inflateOption(DEFAULT_MODES[i], current == DEFAULT_MODES[i], container);
                inflateOption(DEFAULT_MODES[i], current == DEFAULT_MODES[i], container,
                        mBackend.isModeDisallowedByAdmin(DEFAULT_MODES[i]));
            }
        }
    }
@@ -116,15 +127,31 @@ public class UsbModeChooserActivity extends Activity {
        super.onStop();
    }

    private void inflateOption(final int mode, boolean selected, LinearLayout container) {
    private void inflateOption(final int mode, boolean selected, LinearLayout container,
            final boolean disallowedByAdmin) {
        View v = mLayoutInflater.inflate(R.layout.radio_with_summary, container, false);

        ((TextView) v.findViewById(android.R.id.title)).setText(getTitle(mode));
        ((TextView) v.findViewById(android.R.id.summary)).setText(getSummary(mode));
        TextView titleView = (TextView) v.findViewById(android.R.id.title);
        titleView.setText(getTitle(mode));
        TextView summaryView = (TextView) v.findViewById(android.R.id.summary);
        summaryView.setText(getSummary(mode));

        if (disallowedByAdmin) {
            if (mEnforcedAdmin != null) {
                setDisabledByAdmin(titleView, summaryView);
            } else {
                return;
            }
        }

        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (disallowedByAdmin && mEnforcedAdmin != null) {
                    RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
                            UsbModeChooserActivity.this, mEnforcedAdmin);
                    return;
                }
                if (!ActivityManager.isUserAMonkey()) {
                    mBackend.setMode(mode);
                }
@@ -136,6 +163,18 @@ public class UsbModeChooserActivity extends Activity {
        container.addView(v);
    }

    private void setDisabledByAdmin(TextView titleView, TextView summaryView) {
        if (mEnforcedAdmin != null) {
            titleView.setEnabled(false);
            summaryView.setEnabled(false);
            RestrictedLockUtils.setTextViewPadlock(this,
                    titleView, true /* showPadlock */);
            Drawable[] compoundDrawables = titleView.getCompoundDrawablesRelative();
            compoundDrawables[0 /* start */].mutate().setColorFilter(
                    getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
        }
    }

    private static int getSummary(int mode) {
        switch (mode) {
            case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE: