Search in sources :

Example 71 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class Bmgr method backupNowAllPackages.

private void backupNowAllPackages() {
    int userId = UserHandle.USER_SYSTEM;
    IPackageManager mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
    if (mPm == null) {
        System.err.println(PM_NOT_RUNNING_ERR);
        return;
    }
    List<PackageInfo> installedPackages = null;
    try {
        installedPackages = mPm.getInstalledPackages(0, userId).getList();
    } catch (RemoteException e) {
        System.err.println(e.toString());
        System.err.println(PM_NOT_RUNNING_ERR);
    }
    if (installedPackages != null) {
        List<String> packages = new ArrayList<>();
        for (PackageInfo pi : installedPackages) {
            try {
                if (mBmgr.isAppEligibleForBackup(pi.packageName)) {
                    packages.add(pi.packageName);
                }
            } catch (RemoteException e) {
                System.err.println(e.toString());
                System.err.println(BMGR_NOT_RUNNING_ERR);
            }
        }
        backupNowPackages(packages);
    }
}
Also used : IPackageManager(android.content.pm.IPackageManager) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException)

Example 72 with PackageInfo

use of android.content.pm.PackageInfo in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class DeviceAdminAdd method onCreate.

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    mHandler = new Handler(getMainLooper());
    mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mAppOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
    PackageManager packageManager = getPackageManager();
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
        Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
        finish();
        return;
    }
    mIsCalledFromSupportDialog = getIntent().getBooleanExtra(EXTRA_CALLED_FROM_SUPPORT_DIALOG, false);
    String action = getIntent().getAction();
    ComponentName who = (ComponentName) getIntent().getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
    if (who == null) {
        String packageName = getIntent().getStringExtra(EXTRA_DEVICE_ADMIN_PACKAGE_NAME);
        for (ComponentName component : mDPM.getActiveAdmins()) {
            if (component.getPackageName().equals(packageName)) {
                who = component;
                mUninstalling = true;
                break;
            }
        }
        if (who == null) {
            Log.w(TAG, "No component specified in " + action);
            finish();
            return;
        }
    }
    if (action != null && action.equals(DevicePolicyManager.ACTION_SET_PROFILE_OWNER)) {
        setResult(RESULT_CANCELED);
        setFinishOnTouchOutside(true);
        mAddingProfileOwner = true;
        mProfileOwnerName = getIntent().getStringExtra(DevicePolicyManager.EXTRA_PROFILE_OWNER_NAME);
        String callingPackage = getCallingPackage();
        if (callingPackage == null || !callingPackage.equals(who.getPackageName())) {
            Log.e(TAG, "Unknown or incorrect caller");
            finish();
            return;
        }
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(callingPackage, 0);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                Log.e(TAG, "Cannot set a non-system app as a profile owner");
                finish();
                return;
            }
        } catch (NameNotFoundException nnfe) {
            Log.e(TAG, "Cannot find the package " + callingPackage);
            finish();
            return;
        }
    }
    ActivityInfo ai;
    try {
        ai = packageManager.getReceiverInfo(who, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, "Unable to retrieve device policy " + who, e);
        finish();
        return;
    }
    // invalid device admin.
    if (!mDPM.isAdminActive(who)) {
        List<ResolveInfo> avail = packageManager.queryBroadcastReceivers(new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
        int count = avail == null ? 0 : avail.size();
        boolean found = false;
        for (int i = 0; i < count; i++) {
            ResolveInfo ri = avail.get(i);
            if (ai.packageName.equals(ri.activityInfo.packageName) && ai.name.equals(ri.activityInfo.name)) {
                try {
                    // We didn't retrieve the meta data for all possible matches, so
                    // need to use the activity info of this specific one that was retrieved.
                    ri.activityInfo = ai;
                    DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri);
                    found = true;
                } catch (XmlPullParserException e) {
                    Log.w(TAG, "Bad " + ri.activityInfo, e);
                } catch (IOException e) {
                    Log.w(TAG, "Bad " + ri.activityInfo, e);
                }
                break;
            }
        }
        if (!found) {
            Log.w(TAG, "Request to add invalid device admin: " + who);
            finish();
            return;
        }
    }
    ResolveInfo ri = new ResolveInfo();
    ri.activityInfo = ai;
    try {
        mDeviceAdmin = new DeviceAdminInfo(this, ri);
    } catch (XmlPullParserException e) {
        Log.w(TAG, "Unable to retrieve device policy " + who, e);
        finish();
        return;
    } catch (IOException e) {
        Log.w(TAG, "Unable to retrieve device policy " + who, e);
        finish();
        return;
    }
    // "OK" immediately.
    if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) {
        mRefreshing = false;
        if (mDPM.isAdminActive(who)) {
            if (mDPM.isRemovingAdmin(who, android.os.Process.myUserHandle().getIdentifier())) {
                Log.w(TAG, "Requested admin is already being removed: " + who);
                finish();
                return;
            }
            ArrayList<DeviceAdminInfo.PolicyInfo> newPolicies = mDeviceAdmin.getUsedPolicies();
            for (int i = 0; i < newPolicies.size(); i++) {
                DeviceAdminInfo.PolicyInfo pi = newPolicies.get(i);
                if (!mDPM.hasGrantedPolicy(who, pi.ident)) {
                    mRefreshing = true;
                    break;
                }
            }
            if (!mRefreshing) {
                // Nothing changed (or policies were removed) - return immediately
                setResult(Activity.RESULT_OK);
                finish();
                return;
            }
        }
    }
    // need to prompt for permission. Just add and finish.
    if (mAddingProfileOwner && !mDPM.hasUserSetupCompleted()) {
        addAndFinish();
        return;
    }
    mAddMsgText = getIntent().getCharSequenceExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION);
    setContentView(R.layout.device_admin_add);
    mAdminIcon = (ImageView) findViewById(R.id.admin_icon);
    mAdminName = (TextView) findViewById(R.id.admin_name);
    mAdminDescription = (TextView) findViewById(R.id.admin_description);
    mProfileOwnerWarning = (TextView) findViewById(R.id.profile_owner_warning);
    mAddMsg = (TextView) findViewById(R.id.add_msg);
    mAddMsgExpander = (ImageView) findViewById(R.id.add_msg_expander);
    final View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            toggleMessageEllipsis(mAddMsg);
        }
    };
    mAddMsgExpander.setOnClickListener(onClickListener);
    mAddMsg.setOnClickListener(onClickListener);
    // Determine whether the message can be collapsed - getLineCount() gives the correct
    // number of lines only after a layout pass.
    mAddMsg.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            final int maxLines = getEllipsizedLines();
            // hide the icon if number of visible lines does not exceed maxLines
            boolean hideMsgExpander = mAddMsg.getLineCount() <= maxLines;
            mAddMsgExpander.setVisibility(hideMsgExpander ? View.GONE : View.VISIBLE);
            if (hideMsgExpander) {
                mAddMsg.setOnClickListener(null);
                ((View) mAddMsgExpander.getParent()).invalidate();
            }
            mAddMsg.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
    // toggleMessageEllipsis also handles initial layout:
    toggleMessageEllipsis(mAddMsg);
    mAdminWarning = (TextView) findViewById(R.id.admin_warning);
    mAdminPolicies = (ViewGroup) findViewById(R.id.admin_policies);
    mSupportMessage = (TextView) findViewById(R.id.admin_support_message);
    mCancelButton = (Button) findViewById(R.id.cancel_button);
    mCancelButton.setFilterTouchesWhenObscured(true);
    mCancelButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_DECLINED_BY_USER, mDeviceAdmin.getActivityInfo().applicationInfo.uid);
            finish();
        }
    });
    mUninstallButton = (Button) findViewById(R.id.uninstall_button);
    mUninstallButton.setFilterTouchesWhenObscured(true);
    mUninstallButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_UNINSTALLED_BY_USER, mDeviceAdmin.getActivityInfo().applicationInfo.uid);
            mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName());
            finish();
        }
    });
    mActionButton = (Button) findViewById(R.id.action_button);
    mActionButton.setFilterTouchesWhenObscured(true);
    mActionButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (mAdding) {
                addAndFinish();
            } else if (isManagedProfile(mDeviceAdmin) && mDeviceAdmin.getComponent().equals(mDPM.getProfileOwner())) {
                final int userId = UserHandle.myUserId();
                UserDialogs.createRemoveDialog(DeviceAdminAdd.this, userId, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserManager um = UserManager.get(DeviceAdminAdd.this);
                        um.removeUser(userId);
                        finish();
                    }
                }).show();
            } else if (mUninstalling) {
                mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName());
                finish();
            } else if (!mWaitingForRemoveMsg) {
                try {
                    // Don't allow the admin to put a dialog up in front
                    // of us while we interact with the user.
                    ActivityManagerNative.getDefault().stopAppSwitches();
                } catch (RemoteException e) {
                }
                mWaitingForRemoveMsg = true;
                mDPM.getRemoveWarning(mDeviceAdmin.getComponent(), new RemoteCallback(new RemoteCallback.OnResultListener() {

                    @Override
                    public void onResult(Bundle result) {
                        CharSequence msg = result != null ? result.getCharSequence(DeviceAdminReceiver.EXTRA_DISABLE_WARNING) : null;
                        continueRemoveAction(msg);
                    }
                }, mHandler));
                // Don't want to wait too long.
                getWindow().getDecorView().getHandler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        continueRemoveAction(null);
                    }
                }, 2 * 1000);
            }
        }
    });
}
Also used : DialogInterface(android.content.DialogInterface) ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ViewTreeObserver(android.view.ViewTreeObserver) ActivityInfo(android.content.pm.ActivityInfo) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) RemoteCallback(android.os.RemoteCallback) UserManager(android.os.UserManager) DeviceAdminInfo(android.app.admin.DeviceAdminInfo) RemoteException(android.os.RemoteException)

Example 73 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class ActivityThread method handleCreateBackupAgent.

// Instantiate a BackupAgent and tell it that it's alive
private void handleCreateBackupAgent(CreateBackupAgentData data) {
    if (DEBUG_BACKUP)
        Slog.v(TAG, "handleCreateBackupAgent: " + data);
    // Sanity check the requested target package's uid against ours
    try {
        PackageInfo requestedPackage = getPackageManager().getPackageInfo(data.appInfo.packageName, 0, UserHandle.myUserId());
        if (requestedPackage.applicationInfo.uid != Process.myUid()) {
            Slog.w(TAG, "Asked to instantiate non-matching package " + data.appInfo.packageName);
            return;
        }
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
    // no longer idle; we have backup work to do
    unscheduleGcIdler();
    // instantiate the BackupAgent class named in the manifest
    LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
    String packageName = packageInfo.mPackageName;
    if (packageName == null) {
        Slog.d(TAG, "Asked to create backup agent for nonexistent package");
        return;
    }
    String classname = data.appInfo.backupAgentName;
    // full backup operation but no app-supplied agent?  use the default implementation
    if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
        classname = "android.app.backup.FullBackupAgent";
    }
    try {
        IBinder binder = null;
        BackupAgent agent = mBackupAgents.get(packageName);
        if (agent != null) {
            // reusing the existing instance
            if (DEBUG_BACKUP) {
                Slog.v(TAG, "Reusing existing agent instance");
            }
            binder = agent.onBind();
        } else {
            try {
                if (DEBUG_BACKUP)
                    Slog.v(TAG, "Initializing agent class " + classname);
                java.lang.ClassLoader cl = packageInfo.getClassLoader();
                agent = (BackupAgent) cl.loadClass(classname).newInstance();
                // set up the agent's context
                ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
                context.setOuterContext(agent);
                agent.attach(context);
                agent.onCreate();
                binder = agent.onBind();
                mBackupAgents.put(packageName, agent);
            } catch (Exception e) {
                // If this is during restore, fail silently; otherwise go
                // ahead and let the user see the crash.
                Slog.e(TAG, "Agent threw during creation: " + e);
                if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
                    throw e;
                }
            // falling through with 'binder' still null
            }
        }
        // tell the OS that we're live now
        try {
            ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to create BackupAgent " + classname + ": " + e.toString(), e);
    }
}
Also used : IBinder(android.os.IBinder) AndroidRuntimeException(android.util.AndroidRuntimeException) PackageInfo(android.content.pm.PackageInfo) BackupAgent(android.app.backup.BackupAgent) RemoteException(android.os.RemoteException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) AndroidRuntimeException(android.util.AndroidRuntimeException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) SuperNotCalledException(android.util.SuperNotCalledException) TransactionTooLargeException(android.os.TransactionTooLargeException)

Example 74 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class WebViewUpdateServiceTest method createPackageInfo.

private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid, boolean installed, Signature[] signatures, long updateTime) {
    PackageInfo p = createPackageInfo(packageName, enabled, valid, installed);
    p.signatures = signatures;
    p.lastUpdateTime = updateTime;
    return p;
}
Also used : PackageInfo(android.content.pm.PackageInfo)

Example 75 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class ShortcutManagerTest5 method testGetPackageInfo.

public void testGetPackageInfo() {
    PackageInfo pi = mShortcutService.getPackageInfo(mMyPackage, mMyUserId, /*signature*/
    false);
    assertEquals(mMyPackage, pi.packageName);
    assertNull(pi.signatures);
    pi = mShortcutService.getPackageInfo(mMyPackage, mMyUserId, /*signature*/
    true);
    assertEquals(mMyPackage, pi.packageName);
    assertNotNull(pi.signatures);
    pi = mShortcutService.getPackageInfo("no.such.package", mMyUserId, /*signature*/
    true);
    assertNull(pi);
}
Also used : PackageInfo(android.content.pm.PackageInfo)

Aggregations

PackageInfo (android.content.pm.PackageInfo)1567 PackageManager (android.content.pm.PackageManager)703 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)327 ApplicationInfo (android.content.pm.ApplicationInfo)217 Test (org.junit.Test)193 RemoteException (android.os.RemoteException)179 ArrayList (java.util.ArrayList)166 Intent (android.content.Intent)126 IOException (java.io.IOException)121 Context (android.content.Context)81 IPackageManager (android.content.pm.IPackageManager)65 ResolveInfo (android.content.pm.ResolveInfo)65 File (java.io.File)62 SuppressLint (android.annotation.SuppressLint)58 ComponentName (android.content.ComponentName)57 ActivityInfo (android.content.pm.ActivityInfo)57 View (android.view.View)57 TextView (android.widget.TextView)54 Signature (android.content.pm.Signature)51 OverlayInfo (android.content.om.OverlayInfo)42