Search in sources :

Example 16 with UriPermission

use of android.content.UriPermission in project android_frameworks_base by DirtyUnicorns.

the class MtpDocumentsProvider method onCreate.

@Override
public boolean onCreate() {
    sSingleton = this;
    mContext = getContext();
    mResources = getContext().getResources();
    mMtpManager = new MtpManager(getContext());
    mResolver = getContext().getContentResolver();
    mDeviceToolkits = new HashMap<Integer, DeviceToolkit>();
    mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE);
    mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase);
    mAppFuse = new AppFuse(TAG, new AppFuseCallback());
    mIntentSender = new ServiceIntentSender(getContext());
    // after booting.
    try {
        final int bootCount = Settings.Global.getInt(mResolver, Settings.Global.BOOT_COUNT, -1);
        final int lastBootCount = mDatabase.getLastBootCount();
        if (bootCount != -1 && bootCount != lastBootCount) {
            mDatabase.setLastBootCount(bootCount);
            final List<UriPermission> permissions = mResolver.getOutgoingPersistedUriPermissions();
            final Uri[] uris = new Uri[permissions.size()];
            for (int i = 0; i < permissions.size(); i++) {
                uris[i] = permissions.get(i).getUri();
            }
            mDatabase.cleanDatabase(uris);
        }
    } catch (SQLiteDiskIOException error) {
        // It can happen due to disk shortage.
        Log.e(TAG, "Failed to clean database.", error);
        return false;
    }
    // TODO: Mount AppFuse on demands.
    try {
        mAppFuse.mount(getContext().getSystemService(StorageManager.class));
    } catch (IOException error) {
        Log.e(TAG, "Failed to start app fuse.", error);
        return false;
    }
    resume();
    return true;
}
Also used : StorageManager(android.os.storage.StorageManager) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) IOException(java.io.IOException) Uri(android.net.Uri) Point(android.graphics.Point) UriPermission(android.content.UriPermission) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException)

Example 17 with UriPermission

use of android.content.UriPermission in project android_frameworks_base by DirtyUnicorns.

the class OpenExternalDirectoryActivity method getIntentForExistingPermission.

private static Intent getIntentForExistingPermission(OpenExternalDirectoryActivity activity, boolean isRoot, File root, File file) {
    final String packageName = activity.getCallingPackage();
    final ContentProviderClient storageClient = activity.getExternalStorageClient();
    final Uri grantedUri = getGrantedUriPermission(activity, storageClient, file);
    final Uri rootUri = root.equals(file) ? grantedUri : getGrantedUriPermission(activity, storageClient, root);
    if (DEBUG)
        Log.d(TAG, "checking if " + packageName + " already has permission for " + grantedUri + " or its root (" + rootUri + ")");
    final ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
    for (UriPermission uriPermission : am.getGrantedUriPermissions(packageName).getList()) {
        final Uri uri = uriPermission.getUri();
        if (uri == null) {
            Log.w(TAG, "null URI for " + uriPermission);
            continue;
        }
        if (uri.equals(grantedUri) || uri.equals(rootUri)) {
            if (DEBUG)
                Log.d(TAG, packageName + " already has permission: " + uriPermission);
            return createGrantedUriPermissionsIntent(grantedUri);
        }
    }
    if (DEBUG)
        Log.d(TAG, packageName + " does not have permission for " + grantedUri);
    return null;
}
Also used : UriPermission(android.content.UriPermission) ActivityManager(android.app.ActivityManager) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient)

Example 18 with UriPermission

use of android.content.UriPermission in project android_frameworks_base by ResurrectionRemix.

the class MtpDocumentsProvider method onCreate.

@Override
public boolean onCreate() {
    sSingleton = this;
    mContext = getContext();
    mResources = getContext().getResources();
    mMtpManager = new MtpManager(getContext());
    mResolver = getContext().getContentResolver();
    mDeviceToolkits = new HashMap<Integer, DeviceToolkit>();
    mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE);
    mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase);
    mAppFuse = new AppFuse(TAG, new AppFuseCallback());
    mIntentSender = new ServiceIntentSender(getContext());
    // after booting.
    try {
        final int bootCount = Settings.Global.getInt(mResolver, Settings.Global.BOOT_COUNT, -1);
        final int lastBootCount = mDatabase.getLastBootCount();
        if (bootCount != -1 && bootCount != lastBootCount) {
            mDatabase.setLastBootCount(bootCount);
            final List<UriPermission> permissions = mResolver.getOutgoingPersistedUriPermissions();
            final Uri[] uris = new Uri[permissions.size()];
            for (int i = 0; i < permissions.size(); i++) {
                uris[i] = permissions.get(i).getUri();
            }
            mDatabase.cleanDatabase(uris);
        }
    } catch (SQLiteDiskIOException error) {
        // It can happen due to disk shortage.
        Log.e(TAG, "Failed to clean database.", error);
        return false;
    }
    // TODO: Mount AppFuse on demands.
    try {
        mAppFuse.mount(getContext().getSystemService(StorageManager.class));
    } catch (IOException error) {
        Log.e(TAG, "Failed to start app fuse.", error);
        return false;
    }
    resume();
    return true;
}
Also used : StorageManager(android.os.storage.StorageManager) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) IOException(java.io.IOException) Uri(android.net.Uri) Point(android.graphics.Point) UriPermission(android.content.UriPermission) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException)

Example 19 with UriPermission

use of android.content.UriPermission in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AppStorageSettings method refreshGrantedUriPermissions.

private void refreshGrantedUriPermissions() {
    // Clear UI first (in case the activity has been resumed)
    removeUriPermissionsFromUi();
    // Gets all URI permissions from am.
    ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    List<UriPermission> perms = am.getGrantedUriPermissions(mAppEntry.info.packageName).getList();
    if (perms.isEmpty()) {
        mClearUriButton.setVisibility(View.GONE);
        return;
    }
    PackageManager pm = getActivity().getPackageManager();
    // Group number of URIs by app.
    Map<CharSequence, MutableInt> uriCounters = new TreeMap<>();
    for (UriPermission perm : perms) {
        String authority = perm.getUri().getAuthority();
        ProviderInfo provider = pm.resolveContentProvider(authority, 0);
        CharSequence app = provider.applicationInfo.loadLabel(pm);
        MutableInt count = uriCounters.get(app);
        if (count == null) {
            uriCounters.put(app, new MutableInt(1));
        } else {
            count.value++;
        }
    }
    // Dynamically add the preferences, one per app.
    int order = 0;
    for (Map.Entry<CharSequence, MutableInt> entry : uriCounters.entrySet()) {
        int numberResources = entry.getValue().value;
        Preference pref = new Preference(getPrefContext());
        pref.setTitle(entry.getKey());
        pref.setSummary(getPrefContext().getResources().getQuantityString(R.plurals.uri_permissions_text, numberResources, numberResources));
        pref.setSelectable(false);
        pref.setLayoutResource(R.layout.horizontal_preference);
        pref.setOrder(order);
        Log.v(TAG, "Adding preference '" + pref + "' at order " + order);
        mUri.addPreference(pref);
    }
    if (mAppsControlDisallowedBySystem) {
        mClearUriButton.setEnabled(false);
    }
    mClearUri.setOrder(order);
    mClearUriButton.setVisibility(View.VISIBLE);
}
Also used : ActivityManager(android.app.ActivityManager) TreeMap(java.util.TreeMap) PackageManager(android.content.pm.PackageManager) ProviderInfo(android.content.pm.ProviderInfo) Preference(android.support.v7.preference.Preference) MutableInt(android.util.MutableInt) UriPermission(android.content.UriPermission) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 20 with UriPermission

use of android.content.UriPermission in project android_frameworks_base by ResurrectionRemix.

the class ActivityManagerProxy method getGrantedUriPermissions.

@Override
public ParceledListSlice<UriPermission> getGrantedUriPermissions(String packageName, int userId) throws RemoteException {
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    data.writeInterfaceToken(IActivityManager.descriptor);
    data.writeString(packageName);
    data.writeInt(userId);
    mRemote.transact(GET_GRANTED_URI_PERMISSIONS_TRANSACTION, data, reply, 0);
    reply.readException();
    @SuppressWarnings("unchecked") final ParceledListSlice<UriPermission> perms = ParceledListSlice.CREATOR.createFromParcel(reply);
    data.recycle();
    reply.recycle();
    return perms;
}
Also used : Parcel(android.os.Parcel) UriPermission(android.content.UriPermission)

Aggregations

UriPermission (android.content.UriPermission)23 Uri (android.net.Uri)11 Parcel (android.os.Parcel)10 ActivityManager (android.app.ActivityManager)6 IOException (java.io.IOException)6 ContentProviderClient (android.content.ContentProviderClient)5 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)5 Point (android.graphics.Point)5 StorageManager (android.os.storage.StorageManager)5 File (java.io.File)2 ContentResolver (android.content.ContentResolver)1 PackageManager (android.content.pm.PackageManager)1 ProviderInfo (android.content.pm.ProviderInfo)1 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 DocumentFile (android.support.v4.provider.DocumentFile)1 Preference (android.support.v7.preference.Preference)1 MutableInt (android.util.MutableInt)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1