Search in sources :

Example 71 with Uri

use of android.net.Uri in project android_frameworks_base by ParanoidAndroid.

the class Pm method runInstall.

private void runInstall() {
    int installFlags = PackageManager.INSTALL_ALL_USERS;
    String installerPackageName = null;
    String opt;
    String algo = null;
    byte[] iv = null;
    byte[] key = null;
    String macAlgo = null;
    byte[] macKey = null;
    byte[] tag = null;
    String originatingUriString = null;
    String referrer = null;
    while ((opt = nextOption()) != null) {
        if (opt.equals("-l")) {
            installFlags |= PackageManager.INSTALL_FORWARD_LOCK;
        } else if (opt.equals("-r")) {
            installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
        } else if (opt.equals("-i")) {
            installerPackageName = nextOptionData();
            if (installerPackageName == null) {
                System.err.println("Error: no value specified for -i");
                return;
            }
        } else if (opt.equals("-t")) {
            installFlags |= PackageManager.INSTALL_ALLOW_TEST;
        } else if (opt.equals("-s")) {
            // Override if -s option is specified.
            installFlags |= PackageManager.INSTALL_EXTERNAL;
        } else if (opt.equals("-f")) {
            // Override if -s option is specified.
            installFlags |= PackageManager.INSTALL_INTERNAL;
        } else if (opt.equals("-d")) {
            installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE;
        } else if (opt.equals("--algo")) {
            algo = nextOptionData();
            if (algo == null) {
                System.err.println("Error: must supply argument for --algo");
                return;
            }
        } else if (opt.equals("--iv")) {
            iv = hexToBytes(nextOptionData());
            if (iv == null) {
                System.err.println("Error: must supply argument for --iv");
                return;
            }
        } else if (opt.equals("--key")) {
            key = hexToBytes(nextOptionData());
            if (key == null) {
                System.err.println("Error: must supply argument for --key");
                return;
            }
        } else if (opt.equals("--macalgo")) {
            macAlgo = nextOptionData();
            if (macAlgo == null) {
                System.err.println("Error: must supply argument for --macalgo");
                return;
            }
        } else if (opt.equals("--mackey")) {
            macKey = hexToBytes(nextOptionData());
            if (macKey == null) {
                System.err.println("Error: must supply argument for --mackey");
                return;
            }
        } else if (opt.equals("--tag")) {
            tag = hexToBytes(nextOptionData());
            if (tag == null) {
                System.err.println("Error: must supply argument for --tag");
                return;
            }
        } else if (opt.equals("--originating-uri")) {
            originatingUriString = nextOptionData();
            if (originatingUriString == null) {
                System.err.println("Error: must supply argument for --originating-uri");
                return;
            }
        } else if (opt.equals("--referrer")) {
            referrer = nextOptionData();
            if (referrer == null) {
                System.err.println("Error: must supply argument for --referrer");
                return;
            }
        } else {
            System.err.println("Error: Unknown option: " + opt);
            return;
        }
    }
    final ContainerEncryptionParams encryptionParams;
    if (algo != null || iv != null || key != null || macAlgo != null || macKey != null || tag != null) {
        if (algo == null || iv == null || key == null) {
            System.err.println("Error: all of --algo, --iv, and --key must be specified");
            return;
        }
        if (macAlgo != null || macKey != null || tag != null) {
            if (macAlgo == null || macKey == null || tag == null) {
                System.err.println("Error: all of --macalgo, --mackey, and --tag must " + "be specified");
                return;
            }
        }
        try {
            final SecretKey encKey = new SecretKeySpec(key, "RAW");
            final SecretKey macSecretKey;
            if (macKey == null || macKey.length == 0) {
                macSecretKey = null;
            } else {
                macSecretKey = new SecretKeySpec(macKey, "RAW");
            }
            encryptionParams = new ContainerEncryptionParams(algo, new IvParameterSpec(iv), encKey, macAlgo, null, macSecretKey, tag, -1, -1, -1);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
            return;
        }
    } else {
        encryptionParams = null;
    }
    final Uri apkURI;
    final Uri verificationURI;
    final Uri originatingURI;
    final Uri referrerURI;
    if (originatingUriString != null) {
        originatingURI = Uri.parse(originatingUriString);
    } else {
        originatingURI = null;
    }
    if (referrer != null) {
        referrerURI = Uri.parse(referrer);
    } else {
        referrerURI = null;
    }
    // Populate apkURI, must be present
    final String apkFilePath = nextArg();
    System.err.println("\tpkg: " + apkFilePath);
    if (apkFilePath != null) {
        apkURI = Uri.fromFile(new File(apkFilePath));
    } else {
        System.err.println("Error: no package specified");
        return;
    }
    // Populate verificationURI, optionally present
    final String verificationFilePath = nextArg();
    if (verificationFilePath != null) {
        System.err.println("\tver: " + verificationFilePath);
        verificationURI = Uri.fromFile(new File(verificationFilePath));
    } else {
        verificationURI = null;
    }
    PackageInstallObserver obs = new PackageInstallObserver();
    try {
        VerificationParams verificationParams = new VerificationParams(verificationURI, originatingURI, referrerURI, VerificationParams.NO_UID, null);
        mPm.installPackageWithVerificationAndEncryption(apkURI, obs, installFlags, installerPackageName, verificationParams, encryptionParams);
        synchronized (obs) {
            while (!obs.finished) {
                try {
                    obs.wait();
                } catch (InterruptedException e) {
                }
            }
            if (obs.result == PackageManager.INSTALL_SUCCEEDED) {
                System.out.println("Success");
            } else {
                System.err.println("Failure [" + installFailureToString(obs.result) + "]");
            }
        }
    } catch (RemoteException e) {
        System.err.println(e.toString());
        System.err.println(PM_NOT_RUNNING_ERR);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ContainerEncryptionParams(android.content.pm.ContainerEncryptionParams) VerificationParams(android.content.pm.VerificationParams) Uri(android.net.Uri) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) RemoteException(android.os.RemoteException) File(java.io.File) IPackageInstallObserver(android.content.pm.IPackageInstallObserver)

Example 72 with Uri

use of android.net.Uri in project android_frameworks_base by ParanoidAndroid.

the class DefaultDataHandler method startElement.

public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
    if (ROW.equals(localName)) {
        if (mValues != null) {
            // case 2, <Col> before <Row> insert last uri
            if (mUris.empty()) {
                throw new SAXException("uri is empty");
            }
            Uri nextUri = insertRow();
            if (nextUri == null) {
                throw new SAXException("insert to uri " + mUris.lastElement().toString() + " failure");
            } else {
                // make sure the stack lastElement save uri for more than one row
                mUris.pop();
                mUris.push(nextUri);
                parseRow(atts);
            }
        } else {
            int attrLen = atts.getLength();
            if (attrLen == 0) {
                // case 3, share same uri as last level
                mUris.push(mUris.lastElement());
            } else {
                parseRow(atts);
            }
        }
    } else if (COL.equals(localName)) {
        int attrLen = atts.getLength();
        if (attrLen != 2) {
            throw new SAXException("illegal attributes number " + attrLen);
        }
        String key = atts.getValue(0);
        String value = atts.getValue(1);
        if (key != null && key.length() > 0 && value != null && value.length() > 0) {
            if (mValues == null) {
                mValues = new ContentValues();
            }
            mValues.put(key, value);
        } else {
            throw new SAXException("illegal attributes value");
        }
    } else if (DEL.equals(localName)) {
        Uri u = Uri.parse(atts.getValue(URI_STR));
        if (u == null) {
            throw new SAXException("attribute " + atts.getValue(URI_STR) + " parsing failure");
        }
        int attrLen = atts.getLength() - 2;
        if (attrLen > 0) {
            String[] selectionArgs = new String[attrLen];
            for (int i = 0; i < attrLen; i++) {
                selectionArgs[i] = atts.getValue(i + 2);
            }
            mContentResolver.delete(u, atts.getValue(1), selectionArgs);
        } else if (attrLen == 0) {
            mContentResolver.delete(u, atts.getValue(1), null);
        } else {
            mContentResolver.delete(u, null, null);
        }
    } else {
        throw new SAXException("unknown element: " + localName);
    }
}
Also used : Uri(android.net.Uri) SAXException(org.xml.sax.SAXException)

Example 73 with Uri

use of android.net.Uri in project android_frameworks_base by ParanoidAndroid.

the class ContentProviderOperation method apply.

/**
     * Applies this operation using the given provider. The backRefs array is used to resolve any
     * back references that were requested using
     * {@link Builder#withValueBackReferences(ContentValues)} and
     * {@link Builder#withSelectionBackReference}.
     * @param provider the {@link ContentProvider} on which this batch is applied
     * @param backRefs a {@link ContentProviderResult} array that will be consulted
     * to resolve any requested back references.
     * @param numBackRefs the number of valid results on the backRefs array.
     * @return a {@link ContentProviderResult} that contains either the {@link Uri} of the inserted
     * row if this was an insert otherwise the number of rows affected.
     * @throws OperationApplicationException thrown if either the insert fails or
     * if the number of rows affected didn't match the expected count
     */
public ContentProviderResult apply(ContentProvider provider, ContentProviderResult[] backRefs, int numBackRefs) throws OperationApplicationException {
    ContentValues values = resolveValueBackReferences(backRefs, numBackRefs);
    String[] selectionArgs = resolveSelectionArgsBackReferences(backRefs, numBackRefs);
    if (mType == TYPE_INSERT) {
        Uri newUri = provider.insert(mUri, values);
        if (newUri == null) {
            throw new OperationApplicationException("insert failed");
        }
        return new ContentProviderResult(newUri);
    }
    int numRows;
    if (mType == TYPE_DELETE) {
        numRows = provider.delete(mUri, mSelection, selectionArgs);
    } else if (mType == TYPE_UPDATE) {
        numRows = provider.update(mUri, values, mSelection, selectionArgs);
    } else if (mType == TYPE_ASSERT) {
        // Assert that all rows match expected values
        String[] projection = null;
        if (values != null) {
            // Build projection map from expected values
            final ArrayList<String> projectionList = new ArrayList<String>();
            for (Map.Entry<String, Object> entry : values.valueSet()) {
                projectionList.add(entry.getKey());
            }
            projection = projectionList.toArray(new String[projectionList.size()]);
        }
        final Cursor cursor = provider.query(mUri, projection, mSelection, selectionArgs, null);
        try {
            numRows = cursor.getCount();
            if (projection != null) {
                while (cursor.moveToNext()) {
                    for (int i = 0; i < projection.length; i++) {
                        final String cursorValue = cursor.getString(i);
                        final String expectedValue = values.getAsString(projection[i]);
                        if (!TextUtils.equals(cursorValue, expectedValue)) {
                            // Throw exception when expected values don't match
                            Log.e(TAG, this.toString());
                            throw new OperationApplicationException("Found value " + cursorValue + " when expected " + expectedValue + " for column " + projection[i]);
                        }
                    }
                }
            }
        } finally {
            cursor.close();
        }
    } else {
        Log.e(TAG, this.toString());
        throw new IllegalStateException("bad type, " + mType);
    }
    if (mExpectedCount != null && mExpectedCount != numRows) {
        Log.e(TAG, this.toString());
        throw new OperationApplicationException("wrong number of rows: " + numRows);
    }
    return new ContentProviderResult(numRows);
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) Map(java.util.Map) HashMap(java.util.HashMap)

Example 74 with Uri

use of android.net.Uri in project android_frameworks_base by ParanoidAndroid.

the class ContentResolver method insert.

/**
     * Inserts a row into a table at the given URL.
     *
     * If the content provider supports transactions the insertion will be atomic.
     *
     * @param url The URL of the table to insert into.
     * @param values The initial values for the newly inserted row. The key is the column name for
     *               the field. Passing an empty ContentValues will create an empty row.
     * @return the URL of the newly created row.
     */
public final Uri insert(Uri url, ContentValues values) {
    IContentProvider provider = acquireProvider(url);
    if (provider == null) {
        throw new IllegalArgumentException("Unknown URL " + url);
    }
    try {
        long startTime = SystemClock.uptimeMillis();
        Uri createdRow = provider.insert(mPackageName, url, values);
        long durationMillis = SystemClock.uptimeMillis() - startTime;
        maybeLogUpdateToEventLog(durationMillis, url, "insert", null);
        return createdRow;
    } catch (RemoteException e) {
        // Manager will kill this process shortly anyway.
        return null;
    } finally {
        releaseProvider(provider);
    }
}
Also used : RemoteException(android.os.RemoteException) Uri(android.net.Uri)

Example 75 with Uri

use of android.net.Uri in project android_frameworks_base by ParanoidAndroid.

the class DefaultDataHandler method insertRow.

private Uri insertRow() {
    Uri u = mContentResolver.insert(mUris.lastElement(), mValues);
    mValues = null;
    return u;
}
Also used : Uri(android.net.Uri)

Aggregations

Uri (android.net.Uri)3223 Intent (android.content.Intent)682 Cursor (android.database.Cursor)455 File (java.io.File)311 ContentValues (android.content.ContentValues)282 IOException (java.io.IOException)239 ContentResolver (android.content.ContentResolver)236 ArrayList (java.util.ArrayList)197 Test (org.junit.Test)192 RemoteException (android.os.RemoteException)190 Bundle (android.os.Bundle)147 Bitmap (android.graphics.Bitmap)121 Context (android.content.Context)115 InputStream (java.io.InputStream)102 PendingIntent (android.app.PendingIntent)101 LargeTest (android.test.suitebuilder.annotation.LargeTest)97 FileNotFoundException (java.io.FileNotFoundException)91 View (android.view.View)89 Request (android.app.DownloadManager.Request)83 HashMap (java.util.HashMap)69