Search in sources :

Example 1 with ContentProviderClient

use of android.content.ContentProviderClient in project android_frameworks_base by ParanoidAndroid.

the class ActivityTestMain method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, R.style.SlowDialog);
            builder.setTitle("This is a title");
            builder.show();
            return true;
        }
    });
    menu.add("Bind!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Start!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class);
            startService(intent);
            return true;
        }
    });
    menu.add("Send!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserReceiver.class);
            sendOrderedBroadcast(intent, null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Call!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ContentProviderClient cpl = getContentResolver().acquireContentProviderClient(SingleUserProvider.AUTHORITY);
            Bundle res = null;
            try {
                res = cpl.call("getuser", null, null);
            } catch (RemoteException e) {
            }
            int user = res != null ? res.getInt("user", -1) : -1;
            Toast.makeText(ActivityTestMain.this, "Provider executed as user " + (user >= 0 ? Integer.toString(user) : "unknown"), Toast.LENGTH_LONG).show();
            cpl.release();
            return true;
        }
    });
    menu.add("Send to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, UserTarget.class);
            sendOrderedBroadcastAsUser(intent, new UserHandle(0), null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Send to user " + mSecondUser + "!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, UserTarget.class);
            sendOrderedBroadcastAsUser(intent, new UserHandle(mSecondUser), null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Bind to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Bind to user " + mSecondUser + "!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, new UserHandle(mSecondUser))) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Density!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (mOverrideConfig == null) {
                mOverrideConfig = new Configuration();
            }
            if (mOverrideConfig.densityDpi == Configuration.DENSITY_DPI_UNDEFINED) {
                mOverrideConfig.densityDpi = (getApplicationContext().getResources().getConfiguration().densityDpi * 2) / 3;
            } else {
                mOverrideConfig.densityDpi = Configuration.DENSITY_DPI_UNDEFINED;
            }
            recreate();
            return true;
        }
    });
    return true;
}
Also used : AlertDialog(android.app.AlertDialog) ServiceConnection(android.content.ServiceConnection) Configuration(android.content.res.Configuration) Bundle(android.os.Bundle) MenuItem(android.view.MenuItem) Intent(android.content.Intent) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient)

Example 2 with ContentProviderClient

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

the class DocumentsApplication method acquireUnstableProviderOrThrow.

public static ContentProviderClient acquireUnstableProviderOrThrow(ContentResolver resolver, String authority) throws RemoteException {
    final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(authority);
    if (client == null) {
        throw new RemoteException("Failed to acquire provider for " + authority);
    }
    client.setDetectNotResponding(PROVIDER_ANR_TIMEOUT);
    return client;
}
Also used : RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient)

Example 3 with ContentProviderClient

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

the class DocumentInfo method updateFromUri.

public void updateFromUri(ContentResolver resolver, Uri uri) throws FileNotFoundException {
    ContentProviderClient client = null;
    Cursor cursor = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, uri.getAuthority());
        cursor = client.query(uri, null, null, null, null);
        if (!cursor.moveToFirst()) {
            throw new FileNotFoundException("Missing details for " + uri);
        }
        updateFromCursor(cursor, uri.getAuthority());
    } catch (Throwable t) {
        throw asFileNotFoundException(t);
    } finally {
        IoUtils.closeQuietly(cursor);
        ContentProviderClient.releaseQuietly(client);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) ContentProviderClient(android.content.ContentProviderClient)

Example 4 with ContentProviderClient

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

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 5 with ContentProviderClient

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

the class RootsCache method loadRootsForAuthority.

/**
     * Bring up requested provider and query for all active roots.
     */
private Collection<RootInfo> loadRootsForAuthority(ContentResolver resolver, String authority, boolean forceRefresh) {
    if (DEBUG)
        Log.d(TAG, "Loading roots for " + authority);
    synchronized (mObservedAuthorities) {
        if (mObservedAuthorities.add(authority)) {
            // Watch for any future updates
            final Uri rootsUri = DocumentsContract.buildRootsUri(authority);
            mContext.getContentResolver().registerContentObserver(rootsUri, true, mObserver);
        }
    }
    final Uri rootsUri = DocumentsContract.buildRootsUri(authority);
    if (!forceRefresh) {
        // Look for roots data that we might have cached for ourselves in the
        // long-lived system process.
        final Bundle systemCache = resolver.getCache(rootsUri);
        if (systemCache != null) {
            if (DEBUG)
                Log.d(TAG, "System cache hit for " + authority);
            return systemCache.getParcelableArrayList(TAG);
        }
    }
    final ArrayList<RootInfo> roots = new ArrayList<>();
    ContentProviderClient client = null;
    Cursor cursor = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(rootsUri, null, null, null, null);
        while (cursor.moveToNext()) {
            final RootInfo root = RootInfo.fromRootsCursor(authority, cursor);
            roots.add(root);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed to load some roots from " + authority + ": " + e);
    } finally {
        IoUtils.closeQuietly(cursor);
        ContentProviderClient.releaseQuietly(client);
    }
    // Cache these freshly parsed roots over in the long-lived system
    // process, in case our process goes away. The system takes care of
    // invalidating the cache if the package or Uri changes.
    final Bundle systemCache = new Bundle();
    systemCache.putParcelableArrayList(TAG, roots);
    resolver.putCache(rootsUri, systemCache);
    return roots;
}
Also used : RootInfo(com.android.documentsui.model.RootInfo) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient)

Aggregations

ContentProviderClient (android.content.ContentProviderClient)93 RemoteException (android.os.RemoteException)30 Cursor (android.database.Cursor)28 Uri (android.net.Uri)24 ContentResolver (android.content.ContentResolver)18 Bundle (android.os.Bundle)18 FileNotFoundException (java.io.FileNotFoundException)15 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)9 Test (org.junit.Test)9 OperationCanceledException (android.os.OperationCanceledException)8 CancellationSignal (android.os.CancellationSignal)7 IContentProvider (android.content.IContentProvider)6 ActivityManager (android.app.ActivityManager)5 AlertDialog (android.app.AlertDialog)5 ClipData (android.content.ClipData)5 ComponentName (android.content.ComponentName)5 Intent (android.content.Intent)5 ServiceConnection (android.content.ServiceConnection)5 UriPermission (android.content.UriPermission)5