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

Commit aa070915 authored by Eun-Jeong Shin's avatar Eun-Jeong Shin Committed by Android (Google) Code Review
Browse files

Merge "Handling exception when per-use prompt cannot be generated" into tm-dev

parents 0cd984fb f41f0dea
Loading
Loading
Loading
Loading
+66 −23
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.logcat.ILogcatManagerService;
import android.util.Slog;
import android.view.InflateException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@@ -56,33 +57,46 @@ public class LogAccessDialogActivity extends Activity implements
    private String mAlertTitle;
    private AlertDialog.Builder mAlertDialog;
    private AlertDialog mAlert;
    private View mAlertView;

    private static final int DIALOG_TIME_OUT = Build.IS_DEBUGGABLE ? 60000 : 300000;
    private static final int MSG_DISMISS_DIALOG = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            mContext = this;

            // retrieve Intent extra information
            Intent intent = getIntent();
        mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        mUid = intent.getIntExtra("com.android.server.logcat.uid", 0);
        mGid = intent.getIntExtra("com.android.server.logcat.gid", 0);
        mPid = intent.getIntExtra("com.android.server.logcat.pid", 0);
        mFd = intent.getIntExtra("com.android.server.logcat.fd", 0);
            getIntentInfo(intent);

            // retrieve the title string from passed intent extra
            mAlertTitle = getTitleString(mContext, mPackageName, mUid);

        if (mAlertTitle != null) {
            // creaet View
            mAlertView = createView();

            // create AlertDialog
            mAlertDialog = new AlertDialog.Builder(this);
            mAlertDialog.setView(createView());
            mAlertDialog.setView(mAlertView);

            // show Alert
            mAlert = mAlertDialog.create();
            mAlert.show();

            // set Alert Timeout
            mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, DIALOG_TIME_OUT);

        } catch (Exception e) {
            try {
                Slog.e(TAG, "onCreate failed, declining the logd access", e);
                mLogcatManagerService.decline(mUid, mGid, mPid, mFd);
            } catch (RemoteException ex) {
                Slog.e(TAG, "Fails to call remote functions", ex);
            }
        }
    }

@@ -95,6 +109,19 @@ public class LogAccessDialogActivity extends Activity implements
        mAlert = null;
    }

    private void getIntentInfo(Intent intent) throws Exception {

        if (intent == null) {
            throw new NullPointerException("Intent is null");
        }

        mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        mUid = intent.getIntExtra("com.android.server.logcat.uid", 0);
        mGid = intent.getIntExtra("com.android.server.logcat.gid", 0);
        mPid = intent.getIntExtra("com.android.server.logcat.pid", 0);
        mFd = intent.getIntExtra("com.android.server.logcat.fd", 0);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
@@ -116,24 +143,39 @@ public class LogAccessDialogActivity extends Activity implements
        }
    };

    private String getTitleString(Context context, String callingPackage, int uid) {
    private String getTitleString(Context context, String callingPackage, int uid)
            throws Exception {

        PackageManager pm = context.getPackageManager();
        try {
            return context.getString(
                    com.android.internal.R.string.log_access_confirmation_title,
                    pm.getApplicationInfoAsUser(callingPackage,
        if (pm == null) {
            throw new NullPointerException("PackageManager is null");
        }

        CharSequence appLabel = pm.getApplicationInfoAsUser(callingPackage,
                PackageManager.MATCH_DIRECT_BOOT_AUTO,
                            UserHandle.getUserId(uid)).loadLabel(pm));
        } catch (NameNotFoundException e) {
            Slog.e(TAG, "App name is unknown.", e);
            return null;
                UserHandle.getUserId(uid)).loadLabel(pm);
        if (appLabel == null) {
            throw new NameNotFoundException("Application Label is null");
        }

        return context.getString(com.android.internal.R.string.log_access_confirmation_title,
            appLabel);
    }

    private View createView() {
    /**
     * Returns the dialog view.
     * If we cannot retrieve the package name, it returns null and we decline the full device log
     * access
     */
    private View createView() throws Exception {

        final View view = getLayoutInflater().inflate(
                R.layout.log_access_user_consent_dialog_permission, null /*root*/);

        if (view == null) {
            throw new InflateException();
        }

        ((TextView) view.findViewById(R.id.log_access_dialog_title))
            .setText(mAlertTitle);

@@ -144,6 +186,7 @@ public class LogAccessDialogActivity extends Activity implements
        button_deny.setOnClickListener(this);

        return view;

    }

    @Override
+37 −21
Original line number Diff line number Diff line
@@ -102,16 +102,27 @@ public final class LogcatManagerService extends SystemService {
        }
    }

    private void showDialog(int uid, int gid, int pid, int fd) {
    /**
     * Returns the package name.
     * If we cannot retrieve the package name, it returns null and we decline the full device log
     * access
     */
    private String getPackageName(int uid, int gid, int pid, int fd) {

        final ActivityManagerInternal activityManagerInternal =
                LocalServices.getService(ActivityManagerInternal.class);

        PackageManager pm = mContext.getPackageManager();
        if (activityManagerInternal != null) {
            String packageName = activityManagerInternal.getPackageNameByPid(pid);
            if (packageName != null) {
            Intent mIntent = createIntent(packageName, uid, gid, pid, fd);
            mContext.startActivityAsUser(mIntent, UserHandle.SYSTEM);
            return;
                return packageName;
            }
        }

        PackageManager pm = mContext.getPackageManager();
        if (pm == null) {
            // Decline the logd access if PackageManager is null
            Slog.e(TAG, "PackageManager is null, declining the logd access");
            return null;
        }

        String[] packageNames = pm.getPackagesForUid(uid);
@@ -119,21 +130,19 @@ public final class LogcatManagerService extends SystemService {
        if (ArrayUtils.isEmpty(packageNames)) {
            // Decline the logd access if the app name is unknown
            Slog.e(TAG, "Unknown calling package name, declining the logd access");
            declineLogdAccess(uid, gid, pid, fd);
            return;
            return null;
        }

        String firstPackageName = packageNames[0];

        if (firstPackageName.isEmpty() || firstPackageName == null) {
        if (firstPackageName == null || firstPackageName.isEmpty()) {
            // Decline the logd access if the package name from uid is unknown
            Slog.e(TAG, "Unknown calling package name, declining the logd access");
            declineLogdAccess(uid, gid, pid, fd);
            return;
            return null;
        }

        final Intent mIntent = createIntent(firstPackageName, uid, gid, pid, fd);
        mContext.startActivityAsUser(mIntent, UserHandle.SYSTEM);
        return firstPackageName;

    }

    private void declineLogdAccess(int uid, int gid, int pid, int fd) {
@@ -198,19 +207,26 @@ public final class LogcatManagerService extends SystemService {

                final int procState = LocalServices.getService(ActivityManagerInternal.class)
                        .getUidProcessState(mUid);
                // If the process is foreground, show a dialog for user consent
                // If the process is foreground and we can retrieve the package name, show a dialog
                // for user consent
                if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
                    showDialog(mUid, mGid, mPid, mFd);
                } else {
                    String packageName = getPackageName(mUid, mGid, mPid, mFd);
                    if (packageName != null) {
                        final Intent mIntent = createIntent(packageName, mUid, mGid, mPid, mFd);
                        mContext.startActivityAsUser(mIntent, UserHandle.SYSTEM);
                        return;
                    }
                }

                /**
                     * If the process is background, decline the logd access.
                 * If the process is background or cannot retrieve the package name,
                 * decline the logd access.
                 **/
                declineLogdAccess(mUid, mGid, mPid, mFd);
                return;
            }
        }
    }
    }

    public LogcatManagerService(Context context) {
        super(context);