FileDocCategorySizeDatePackage
GearsSettingsDialog.javaAPI DocAndroid 1.5 API15877Wed May 06 22:42:42 BST 2009com.android.browser

GearsSettingsDialog

public class GearsSettingsDialog extends GearsBaseDialog implements com.android.browser.GearsPermissions.PermissionsChangesListener
Gears Settings dialog

Fields Summary
private static final String
TAG
private Vector
mSitesPermissions
private Vector
mOriginalPermissions
private Vector
mCurrentPermissions
private Vector
mPermissions
private static final int
CONFIRMATION_REMOVE_DIALOG
private final com.android.browser.GearsPermissions.PermissionType
LOCAL_STORAGE
private final com.android.browser.GearsPermissions.PermissionType
LOCATION_DATA
private boolean
mChanges
SettingsAdapter
mListAdapter
Constructors Summary
public GearsSettingsDialog(android.app.Activity activity, android.os.Handler handler, String arguments)


    
                              
                               
    super (activity, handler, arguments);
    activity.setContentView(R.layout.gears_settings);
  
Methods Summary
public java.lang.StringcloseDialog(int closingType)

    String ret = computeDiff(mChanges);

    if (mDebug) {
      printPermissions();
    }

    return ret;
  
public java.lang.StringcomputeDiff(boolean modif)
Computes the difference between the original permissions and the current ones. Returns a json-formatted string. It is used by the Settings dialog.

    String ret = null;
    try {
      JSONObject results = new JSONObject();
      JSONArray permissions = new JSONArray();

      for (int i = 0; modif && i < mOriginalPermissions.size(); i++) {
        OriginPermissions original = mOriginalPermissions.get(i);
        OriginPermissions current = mCurrentPermissions.get(i);
        JSONObject permission = new JSONObject();
        boolean modifications = false;

        for (int j = 0; j < mPermissions.size(); j++) {
          PermissionType type = mPermissions.get(j);

          if (current.getPermission(type) != original.getPermission(type)) {
            JSONObject state = new JSONObject();
            state.put("permissionState", current.getPermission(type));
            permission.put(type.getName(), state);
            modifications = true;
          }
        }

        if (modifications) {
          permission.put("name", current.getOrigin());
          permissions.put(permission);
        }
      }
      results.put("modifiedOrigins", permissions);
      ret = results.toString();
    } catch (JSONException e) {
      Log.e(TAG, "JSON exception ", e);
    }
    return ret;
  
public booleanhandleBackButton()

    return mListAdapter.backButtonPressed();
  
public android.app.DialogonCreateDialog(int id)
We use this to create a confirmation dialog when the user clicks on "remove this site from gears"

    return new AlertDialog.Builder(mActivity)
        .setTitle(R.string.settings_confirmation_remove_title)
        .setMessage(R.string.settings_confirmation_remove)
        .setPositiveButton(android.R.string.ok,
                           new AlertDialog.OnClickListener() {
          public void onClick(DialogInterface dlg, int which) {
            mListAdapter.removeCurrentSite();
          }
        })
        .setNegativeButton(android.R.string.cancel, null)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .create();
  
public voidprintPermissions()
Utility method used in debug mode to print the list of permissions (original values and current values).

    Log.v(TAG, "Original Permissions: ");
    for (int i = 0; i < mOriginalPermissions.size(); i++) {
      OriginPermissions p = mOriginalPermissions.get(i);
      p.print();
    }
    Log.v(TAG, "Current Permissions: ");
    for (int i = 0; i < mSitesPermissions.size(); i++) {
      OriginPermissions p = mSitesPermissions.get(i);
      p.print();
    }
  
private voidsetMainTitle()

    String windowTitle = mActivity.getString(R.string.pref_extras_gears_settings);
    mActivity.setTitle(windowTitle);
  
public booleansetPermission(com.android.browser.GearsPermissions.PermissionType type, int perm)
GearsPermissions.PermissionsChangesListener delegate

    if (mChanges == false) {
      mChanges = true;
    }
    return mChanges;
  
public voidsetup()

    // First let's add the permissions' resources
    LOCAL_STORAGE.setResources(R.string.settings_storage_title,
                               R.string.settings_storage_subtitle_on,
                               R.string.settings_storage_subtitle_off);
    LOCATION_DATA.setResources(R.string.settings_location_title,
                               R.string.settings_location_subtitle_on,
                               R.string.settings_location_subtitle_off);
    // add the permissions to the list of permissions.
    mPermissions = new Vector<PermissionType>();
    mPermissions.add(LOCAL_STORAGE);
    mPermissions.add(LOCATION_DATA);
    OriginPermissions.setListener(this);


    setupDialog();

    // We manage the permissions using three vectors, mSitesPermissions,
    // mOriginalPermissions and mCurrentPermissions.
    // The dialog's arguments are parsed and a list of permissions is
    // generated and stored in those three vectors.
    // mOriginalPermissions is a separate copy and will not be modified;
    // mSitesPermissions contains the current permissions _only_ --
    // if an origin is removed, it is also removed from mSitesPermissions.
    // Finally, mCurrentPermissions contains the current permissions and
    // is a clone of mSitesPermissions, but removed sites aren't removed,
    // their permissions are simply set to PERMISSION_NOT_SET. This
    // allows us to easily generate the final difference between the
    // original permissions and the final permissions, while directly
    // using mSitesPermissions for the listView adapter (SettingsAdapter).

    mSitesPermissions = new Vector<OriginPermissions>();
    mOriginalPermissions = new Vector<OriginPermissions>();

    try {
      JSONObject json = new JSONObject(mDialogArguments);
      if (json.has("permissions")) {
        JSONArray jsonArray = json.getJSONArray("permissions");
        for (int i = 0; i < jsonArray.length(); i++) {
          JSONObject infos = jsonArray.getJSONObject(i);
          String name = null;
          int localStorage = PermissionType.PERMISSION_NOT_SET;
          int locationData = PermissionType.PERMISSION_NOT_SET;
          if (infos.has("name")) {
            name = infos.getString("name");
          }
          if (infos.has(LOCAL_STORAGE_STRING)) {
            JSONObject perm = infos.getJSONObject(LOCAL_STORAGE_STRING);
            if (perm.has("permissionState")) {
              localStorage = perm.getInt("permissionState");
            }
          }
          if (infos.has(LOCATION_DATA_STRING)) {
            JSONObject perm = infos.getJSONObject(LOCATION_DATA_STRING);
            if (perm.has("permissionState")) {
              locationData = perm.getInt("permissionState");
            }
          }
          OriginPermissions perms = new OriginPermissions(name);
          perms.setPermission(LOCAL_STORAGE, localStorage);
          perms.setPermission(LOCATION_DATA, locationData);

          mSitesPermissions.add(perms);
          mOriginalPermissions.add(new OriginPermissions(perms));
        }
      }
    } catch (JSONException e) {
      Log.e(TAG, "JSON exception ", e);
    }
    mCurrentPermissions = (Vector<OriginPermissions>)mSitesPermissions.clone();

    View listView = findViewById(R.id.sites_list);
    if (listView != null) {
      ListView list = (ListView) listView;
      mListAdapter = new SettingsAdapter(mActivity, mSitesPermissions);
      list.setAdapter(mListAdapter);
      list.setScrollBarStyle(android.view.View.SCROLLBARS_OUTSIDE_INSET);
      list.setOnItemClickListener(mListAdapter);
    }
    if (mDebug) {
      printPermissions();
    }
  
public voidsetupDialog()

    setMainTitle();