Search in sources :

Example 41 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method restoreEntity.

/**
     * Restore account sync settings from the given data input stream.
     */
@Override
public void restoreEntity(BackupDataInputStream data) {
    byte[] dataBytes = new byte[data.size()];
    try {
        // Read the data and convert it to a String.
        data.read(dataBytes);
        String dataString = new String(dataBytes, JSON_FORMAT_ENCODING);
        // Convert data to a JSON object.
        JSONObject dataJSON = new JSONObject(dataString);
        boolean masterSyncEnabled = dataJSON.getBoolean(KEY_MASTER_SYNC_ENABLED);
        JSONArray accountJSONArray = dataJSON.getJSONArray(KEY_ACCOUNTS);
        boolean currentMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
        if (currentMasterSyncEnabled) {
            // Disable master sync to prevent any syncs from running.
            ContentResolver.setMasterSyncAutomatically(false);
        }
        try {
            restoreFromJsonArray(accountJSONArray);
        } finally {
            // Set the master sync preference to the value from the backup set.
            ContentResolver.setMasterSyncAutomatically(masterSyncEnabled);
        }
        Log.i(TAG, "Restore successful.");
    } catch (IOException | JSONException e) {
        Log.e(TAG, "Couldn't restore account sync settings\n" + e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 42 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method restoreExistingAccountSyncSettingsFromJSON.

/**
     * Restore account sync settings using the given JSON. This function won't work if the account
     * doesn't exist yet.
     * This function will only be called during Setup Wizard, where we are guaranteed that there
     * are no active syncs.
     * There are 2 pieces of data to restore -
     *      isSyncable (corresponds to {@link ContentResolver#getIsSyncable(Account, String)}
     *      syncEnabled (corresponds to {@link ContentResolver#getSyncAutomatically(Account, String)}
     * <strong>The restore favours adapters that were enabled on the old device, and doesn't care
     * about adapters that were disabled.</strong>
     *
     * syncEnabled=true in restore data.
     * syncEnabled will be true on this device. isSyncable will be left as the default in order to
     * give the enabled adapter the chance to run an initialization sync.
     *
     * syncEnabled=false in restore data.
     * syncEnabled will be false on this device. isSyncable will be set to 2, unless it was 0 on the
     * old device in which case it will be set to 0 on this device. This is because isSyncable=0 is
     * a rare state and was probably set to 0 for good reason (historically isSyncable is a way by
     * which adapters control their own sync state independently of sync settings which is
     * toggleable by the user).
     * isSyncable=2 is a new isSyncable state we introduced specifically to allow adapters that are
     * disabled after a restore to run initialization logic when the adapter is later enabled.
     * See com.android.server.content.SyncStorageEngine#setSyncAutomatically
     *
     * The end result is that an adapter that the user had on will be turned on and get an
     * initialization sync, while an adapter that the user had off will be off until the user
     * enables it on this device at which point it will get an initialization sync.
     */
private void restoreExistingAccountSyncSettingsFromJSON(JSONObject accountJSON) throws JSONException {
    // Restore authorities.
    JSONArray authorities = accountJSON.getJSONArray(KEY_ACCOUNT_AUTHORITIES);
    String accountName = accountJSON.getString(KEY_ACCOUNT_NAME);
    String accountType = accountJSON.getString(KEY_ACCOUNT_TYPE);
    final Account account = new Account(accountName, accountType);
    for (int i = 0; i < authorities.length(); i++) {
        JSONObject authority = (JSONObject) authorities.get(i);
        final String authorityName = authority.getString(KEY_AUTHORITY_NAME);
        boolean wasSyncEnabled = authority.getBoolean(KEY_AUTHORITY_SYNC_ENABLED);
        int wasSyncable = authority.getInt(KEY_AUTHORITY_SYNC_STATE);
        ContentResolver.setSyncAutomaticallyAsUser(account, authorityName, wasSyncEnabled, 0);
        if (!wasSyncEnabled) {
            ContentResolver.setIsSyncable(account, authorityName, wasSyncable == 0 ? 0 : /* not syncable */
            2);
        }
    }
}
Also used : Account(android.accounts.Account) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 43 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class AccountSyncSettingsBackupHelper method accountAddedInternal.

/**
     * Restore SyncSettings for all existing accounts from a stashed backup-set
     */
private void accountAddedInternal() {
    String jsonString;
    try (FileInputStream fIn = new FileInputStream(new File(STASH_FILE))) {
        DataInputStream in = new DataInputStream(fIn);
        jsonString = in.readUTF();
    } catch (FileNotFoundException fnfe) {
        // This is expected to happen when there is no accounts info stashed
        if (DEBUG)
            Log.d(TAG, "unable to find the stash file", fnfe);
        return;
    } catch (IOException ioe) {
        if (DEBUG)
            Log.d(TAG, "could not read sync settings from stash file", ioe);
        return;
    }
    try {
        JSONArray unaddedAccountsJSONArray = new JSONArray(jsonString);
        restoreFromJsonArray(unaddedAccountsJSONArray);
    } catch (JSONException jse) {
        // Malformed jsonString
        Log.e(TAG, "there was an error with the stashed sync settings", jse);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BackupDataInputStream(android.app.backup.BackupDataInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 44 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class AndroidAppAsset method create.

/**
     * Checks that the input is a valid Android app asset.
     *
     * @param asset a JSONObject that has "namespace", "package_name", and
     *              "sha256_cert_fingerprints" fields.
     * @throws AssociationServiceException if the asset is not well formatted.
     */
public static AndroidAppAsset create(JSONObject asset) throws AssociationServiceException {
    String packageName = asset.optString(Utils.ANDROID_APP_ASSET_FIELD_PACKAGE_NAME);
    if (packageName.equals("")) {
        throw new AssociationServiceException(String.format(MISSING_FIELD_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_PACKAGE_NAME));
    }
    JSONArray certArray = asset.optJSONArray(Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS);
    if (certArray == null || certArray.length() == 0) {
        throw new AssociationServiceException(String.format(MISSING_APPCERTS_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS));
    }
    List<String> certFingerprints = new ArrayList<>(certArray.length());
    for (int i = 0; i < certArray.length(); i++) {
        try {
            certFingerprints.add(certArray.getString(i));
        } catch (JSONException e) {
            throw new AssociationServiceException(String.format(APPCERT_NOT_STRING_FORMAT_STRING, Utils.ANDROID_APP_ASSET_FIELD_CERT_FPS));
        }
    }
    return new AndroidAppAsset(packageName, certFingerprints);
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 45 with JSONArray

use of org.json.JSONArray in project platform_frameworks_base by android.

the class JsonParser method parse.

/**
     * Consumes and parses exactly one JSON object from the {@link JsonReader}.
     * The object's fields can only be objects, strings or arrays of strings.
     */
public static JSONObject parse(JsonReader reader) throws IOException, JSONException {
    JSONObject output = new JSONObject();
    String errorMsg = null;
    reader.beginObject();
    while (reader.hasNext()) {
        String fieldName = reader.nextName();
        if (output.has(fieldName)) {
            errorMsg = "Duplicate field name.";
            reader.skipValue();
            continue;
        }
        JsonToken token = reader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            output.put(fieldName, new JSONArray(parseArray(reader)));
        } else if (token.equals(JsonToken.STRING)) {
            output.put(fieldName, reader.nextString());
        } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
            try {
                output.put(fieldName, parse(reader));
            } catch (JSONException e) {
                errorMsg = e.getMessage();
            }
        } else {
            reader.skipValue();
            errorMsg = "Unsupported value type.";
        }
    }
    reader.endObject();
    if (errorMsg != null) {
        throw new JSONException(errorMsg);
    }
    return output;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(android.util.JsonToken)

Aggregations

JSONArray (org.json.JSONArray)1710 JSONObject (org.json.JSONObject)1191 JSONException (org.json.JSONException)738 ArrayList (java.util.ArrayList)323 IOException (java.io.IOException)243 Test (org.junit.Test)207 HashMap (java.util.HashMap)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)96 List (java.util.List)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 File (java.io.File)54 HashSet (java.util.HashSet)54 Date (java.util.Date)47 Query (org.b3log.latke.repository.Query)47 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 GraphObject (com.abewy.android.apps.klyph.core.graph.GraphObject)43 FileNotFoundException (java.io.FileNotFoundException)40 Map (java.util.Map)40 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)36 VolleyError (com.android.volley.VolleyError)35