Search in sources :

Example 51 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ShadowContentResolver method getContentProviderClient.

private ContentProviderClient getContentProviderClient(ContentProvider provider, boolean stable) {
    ContentProviderClient client = Shadow.newInstance(ContentProviderClient.class, new Class[] { ContentResolver.class, IContentProvider.class, boolean.class }, new Object[] { realContentResolver, provider.getIContentProvider(), stable });
    shadowOf(client).setContentProvider(provider);
    return client;
}
Also used : ContentProviderClient(android.content.ContentProviderClient)

Example 52 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ContentProviderControllerTest method withoutManifest_shouldRegisterWithContentResolver.

@Test
public void withoutManifest_shouldRegisterWithContentResolver() throws Exception {
    ProviderInfo providerInfo = new ProviderInfo();
    providerInfo.authority = "some-authority";
    controller.create(providerInfo);
    ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
    ContentProviderClient client = contentResolver.acquireContentProviderClient(providerInfo.authority);
    client.query(Uri.parse("something"), new String[] { "title" }, "*", new String[] {}, "created");
    assertThat(controller.get().transcript).containsExactly("onCreate", "query for something");
}
Also used : ProviderInfo(android.content.pm.ProviderInfo) ContentProviderClient(android.content.ContentProviderClient) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 53 with ContentProviderClient

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

the class DirectoryLoader method loadInBackground.

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }
        mSignal = new CancellationSignal();
    }
    final ContentResolver resolver = getContext().getContentResolver();
    final String authority = mUri.getAuthority();
    final DirectoryResult result = new DirectoryResult();
    result.doc = mDoc;
    // Use default document when searching
    if (mSearchMode) {
        final Uri docUri = DocumentsContract.buildDocumentUri(mRoot.authority, mRoot.documentId);
        try {
            mDoc = DocumentInfo.fromUri(resolver, docUri);
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to query", e);
            result.exception = e;
            return result;
        }
    }
    if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) {
        result.sortOrder = mUserSortOrder;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
            result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
        } else {
            result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
        }
    }
    // Search always uses ranking from provider
    if (mSearchMode) {
        result.sortOrder = State.SORT_ORDER_UNKNOWN;
    }
    if (DEBUG)
        Log.d(TAG, "userSortOrder=" + mUserSortOrder + ", sortOrder=" + result.sortOrder);
    ContentProviderClient client = null;
    Cursor cursor = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
        if (cursor == null) {
            throw new RemoteException("Provider returned null");
        }
        cursor.registerContentObserver(mObserver);
        cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
        if (mSearchMode) {
            // Filter directories out of search results, for now
            cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
        }
        result.client = client;
        result.cursor = cursor;
    } catch (Exception e) {
        Log.w(TAG, "Failed to query", e);
        result.exception = e;
        ContentProviderClient.releaseQuietly(client);
    } finally {
        synchronized (this) {
            mSignal = null;
        }
    }
    return result;
}
Also used : OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) OperationCanceledException(android.os.OperationCanceledException) RemoteException(android.os.RemoteException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) CancellationSignal(android.os.CancellationSignal) ContentProviderClient(android.content.ContentProviderClient)

Example 54 with ContentProviderClient

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

the class DocumentClipper method getDocumentsFromClipData.

/**
     * Returns a list of Documents as decoded in clipData.
     * This should be run from inside an AsyncTask.
     */
public List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
    assert (clipData != null);
    final List<DocumentInfo> srcDocs = new ArrayList<>();
    int count = clipData.getItemCount();
    if (count == 0) {
        return srcDocs;
    }
    ContentResolver resolver = mContext.getContentResolver();
    for (int i = 0; i < count; ++i) {
        ClipData.Item item = clipData.getItemAt(i);
        Uri itemUri = item.getUri();
        if (itemUri != null && DocumentsContract.isDocumentUri(mContext, itemUri)) {
            ContentProviderClient client = null;
            Cursor cursor = null;
            try {
                client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, itemUri.getAuthority());
                cursor = client.query(itemUri, null, null, null, null);
                cursor.moveToPosition(0);
                srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            } finally {
                IoUtils.closeQuietly(cursor);
                ContentProviderClient.releaseQuietly(client);
            }
        }
    }
    return srcDocs;
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) ClipData(android.content.ClipData) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient) DocumentInfo(com.android.documentsui.model.DocumentInfo) ContentResolver(android.content.ContentResolver)

Example 55 with ContentProviderClient

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

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("Rebind Isolated!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

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

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

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Isolated service disconnected " + name);
                }
            };
            if (mIsolatedConnection != null) {
                Log.i(TAG, "Unbinding existing service: " + mIsolatedConnection);
                unbindService(mIsolatedConnection);
                mIsolatedConnection = null;
            }
            Log.i(TAG, "Binding new service: " + conn);
            if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
                mIsolatedConnection = conn;
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            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.SYSTEM)) {
                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;
        }
    });
    menu.add("HashArray").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ArrayMapTests.run();
            return true;
        }
    });
    menu.add("Add App Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            addAppRecents(1);
            return true;
        }
    });
    menu.add("Add App 10x Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            addAppRecents(10);
            return true;
        }
    });
    menu.add("Exclude!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            setExclude(true);
            return true;
        }
    });
    menu.add("Include!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            setExclude(false);
            return true;
        }
    });
    menu.add("Open Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ActivityManager.AppTask task = findDocTask();
            if (task == null) {
                Intent intent = new Intent(ActivityTestMain.this, DocActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
                startActivity(intent);
            } else {
                task.moveToFront();
            }
            return true;
        }
    });
    menu.add("Stack Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ActivityManager.AppTask task = findDocTask();
            if (task != null) {
                ActivityManager.RecentTaskInfo recent = task.getTaskInfo();
                Intent intent = new Intent(ActivityTestMain.this, DocActivity.class);
                if (recent.id >= 0) {
                    // Stack on top.
                    intent.putExtra(DocActivity.LABEL, "Stacked");
                } else {
                    // Start root activity.
                    intent.putExtra(DocActivity.LABEL, "New Root");
                }
                task.startActivity(ActivityTestMain.this, intent, null);
            }
            return true;
        }
    });
    menu.add("Spam!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            scheduleSpam(false);
            return true;
        }
    });
    menu.add("Track time").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "We are sharing this with you!");
            ActivityOptions options = ActivityOptions.makeBasic();
            Intent receiveIntent = new Intent(ActivityTestMain.this, TrackTimeReceiver.class);
            receiveIntent.putExtra("something", "yeah, this is us!");
            options.requestUsageTimeReport(PendingIntent.getBroadcast(ActivityTestMain.this, 0, receiveIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            startActivity(Intent.createChooser(intent, "Who do you love?"), options.toBundle());
            return true;
        }
    });
    menu.add("Transaction fail").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.putExtra("gulp", new int[1024 * 1024]);
            startActivity(intent);
            return true;
        }
    });
    menu.add("Spam idle alarm").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            scheduleSpamAlarm(0);
            return true;
        }
    });
    menu.add("Ignore battery optimizations").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent;
            if (!mPower.isIgnoringBatteryOptimizations(getPackageName())) {
                intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.fromParts("package", getPackageName(), null));
            } else {
                intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            }
            startActivity(intent);
            return true;
        }
    });
    return true;
}
Also used : AlertDialog(android.app.AlertDialog) ServiceConnection(android.content.ServiceConnection) Configuration(android.content.res.Configuration) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) ActivityOptions(android.app.ActivityOptions) Bundle(android.os.Bundle) MenuItem(android.view.MenuItem) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient)

Aggregations

ContentProviderClient (android.content.ContentProviderClient)66 Cursor (android.database.Cursor)23 Uri (android.net.Uri)21 RemoteException (android.os.RemoteException)21 ContentResolver (android.content.ContentResolver)15 ArrayList (java.util.ArrayList)12 Bundle (android.os.Bundle)11 FileNotFoundException (java.io.FileNotFoundException)11 Test (org.junit.Test)7 CancellationSignal (android.os.CancellationSignal)6 ActivityManager (android.app.ActivityManager)5 AlertDialog (android.app.AlertDialog)5 ClipData (android.content.ClipData)5 ComponentName (android.content.ComponentName)5 IContentProvider (android.content.IContentProvider)5 Intent (android.content.Intent)5 ServiceConnection (android.content.ServiceConnection)5 UriPermission (android.content.UriPermission)5 Configuration (android.content.res.Configuration)5 MediaInserter (android.media.MediaInserter)5