From 1bdaf3672e0cde88e779915d972beabae1e9360c Mon Sep 17 00:00:00 2001 From: peli0101 Date: Sat, 17 Jan 2009 16:38:06 +0000 Subject: [PATCH] OI Safe: Fix issue 170. Include a new dialog AllowExternalAccessDialog that is prompted if an external application tries to access OI Safe. It is only asked after the password has been entered. git-svn-id: http://openintents.googlecode.com/svn/trunk/Safe@1753 72b678ce-9140-0410-bee8-679b907dd61a --- AndroidManifest.xml | 7 +- res/layout/dialog_allow_access.xml | 38 +++++++++ res/values/strings.xml | 3 + src/org/openintents/safe/FrontDoor.java | 53 +++++++++--- src/org/openintents/safe/Preferences.java | 2 + .../dialog/AllowExternalAccessDialog.java | 80 +++++++++++++++++++ .../safe/dialog/DialogHostingActivity.java | 8 +- 7 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 res/layout/dialog_allow_access.xml create mode 100644 src/org/openintents/safe/dialog/AllowExternalAccessDialog.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 329b9d8..28e173a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -26,7 +26,8 @@ - + @@ -61,7 +62,9 @@ - + + diff --git a/res/layout/dialog_allow_access.xml b/res/layout/dialog_allow_access.xml new file mode 100644 index 0000000..725b4a6 --- /dev/null +++ b/res/layout/dialog_allow_access.xml @@ -0,0 +1,38 @@ + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index d0aad5f..8a06cb5 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -122,6 +122,9 @@ Preferences Allow external access Allow other applications to access safe. + External application tries to access OI Safe + Allow other applications to access OI Safe. + This can also be set in Menu / Preferences. Lock timeout Minutes before timeout occurs and safe is locked. Lock timeout diff --git a/src/org/openintents/safe/FrontDoor.java b/src/org/openintents/safe/FrontDoor.java index ff3adab..4d7ce4d 100644 --- a/src/org/openintents/safe/FrontDoor.java +++ b/src/org/openintents/safe/FrontDoor.java @@ -20,6 +20,7 @@ package org.openintents.safe; import java.util.ArrayList; import org.openintents.intents.CryptoIntents; +import org.openintents.safe.dialog.DialogHostingActivity; import org.openintents.safe.service.ServiceDispatch; import org.openintents.safe.service.ServiceDispatchImpl; @@ -51,7 +52,7 @@ public class FrontDoor extends Activity { private static String TAG = "FrontDoor"; private static final int REQUEST_CODE_ASK_PASSWORD = 1; - private static final int REQUEST_CODE_GRANT_EXTERNAL_ACCESS = 2; + private static final int REQUEST_CODE_ALLOW_EXTERNAL_ACCESS = 2; private DBHelper dbHelper; @@ -93,10 +94,20 @@ public class FrontDoor extends Activity { // TODO Auto-generated catch block e1.printStackTrace(); } - actionDispatch(); + + boolean externalAccess = mPreferences.getBoolean(Preferences.PREFERENCE_ALLOW_EXTERNAL_ACCESS, false); + boolean isLocal = isIntentLocal(); + + if (isLocal || externalAccess) { + actionDispatch(); + } else { + // ask first + showDialogAllowExternalAccess(); + } break; - case REQUEST_CODE_GRANT_EXTERNAL_ACCESS: - + case REQUEST_CODE_ALLOW_EXTERNAL_ACCESS: + + actionDispatch(); break; } @@ -105,6 +116,15 @@ public class FrontDoor extends Activity { finish(); } } + + /** + * + */ + private void showDialogAllowExternalAccess() { + Intent i = new Intent(this, DialogHostingActivity.class); + i.putExtra(DialogHostingActivity.EXTRA_DIALOG_ID, DialogHostingActivity.DIALOG_ID_ALLOW_EXTERNAL_ACCESS); + this.startActivityForResult(i, REQUEST_CODE_ALLOW_EXTERNAL_ACCESS); + } protected void actionDispatch () { final Intent thisIntent = getIntent(); @@ -118,7 +138,7 @@ public class FrontDoor extends Activity { ch.setPassword(masterKey); } - boolean externalAccess = mPreferences.getBoolean("external_access", false); + boolean externalAccess = mPreferences.getBoolean(Preferences.PREFERENCE_ALLOW_EXTERNAL_ACCESS, false); if (action == null || action.equals(Intent.ACTION_MAIN)){ //TODO: When launched from debugger, action is null. Other such cases? @@ -356,8 +376,7 @@ public class FrontDoor extends Activity { //--------------------------- service stuff ------------ private void initService() { - String action = getIntent().getAction(); - boolean isLocal = action == null || action.equals(Intent.ACTION_MAIN); + boolean isLocal = isIntentLocal(); conn = new ServiceDispatchConnection(isLocal); Intent i = new Intent(); i.setClass(this, ServiceDispatchImpl.class); @@ -365,6 +384,15 @@ public class FrontDoor extends Activity { bindService( i, conn, Context.BIND_AUTO_CREATE); } + /** + * @return + */ + private boolean isIntentLocal() { + String action = getIntent().getAction(); + boolean isLocal = action == null || action.equals(Intent.ACTION_MAIN); + return isLocal; + } + private void releaseService() { if (conn != null ) { unbindService( conn ); @@ -410,8 +438,15 @@ public class FrontDoor extends Activity { } else { if (debug) Log.d(TAG, "service already started"); //service already started, so don't need to ask pw. - masterKey = service.getPassword(); - actionDispatch(); + + boolean externalAccess = mPreferences.getBoolean(Preferences.PREFERENCE_ALLOW_EXTERNAL_ACCESS, false); + + if (askPassIsLocal || externalAccess) { + masterKey = service.getPassword(); + actionDispatch(); + } else { + showDialogAllowExternalAccess(); + } } } catch (RemoteException e) { Log.d(TAG, e.toString()); diff --git a/src/org/openintents/safe/Preferences.java b/src/org/openintents/safe/Preferences.java index 5047028..6161c64 100644 --- a/src/org/openintents/safe/Preferences.java +++ b/src/org/openintents/safe/Preferences.java @@ -5,6 +5,8 @@ import android.preference.PreferenceActivity; public class Preferences extends PreferenceActivity { + public static final String PREFERENCE_ALLOW_EXTERNAL_ACCESS = "external_access"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/src/org/openintents/safe/dialog/AllowExternalAccessDialog.java b/src/org/openintents/safe/dialog/AllowExternalAccessDialog.java new file mode 100644 index 0000000..f46ae0c --- /dev/null +++ b/src/org/openintents/safe/dialog/AllowExternalAccessDialog.java @@ -0,0 +1,80 @@ +package org.openintents.safe.dialog; + +import org.openintents.safe.Preferences; +import org.openintents.safe.R; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.content.DialogInterface.OnClickListener; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CheckBox; + +public class AllowExternalAccessDialog extends AlertDialog implements OnClickListener { + private static final String TAG = "FilenameDialog"; + + private static final String BUNDLE_TAGS = "tags"; + + protected static final int DIALOG_ID_NO_FILE_MANAGER_AVAILABLE = 2; + + Context mContext; + + CheckBox mCheckBox; + + public AllowExternalAccessDialog(Context context) { + super(context); + mContext = context; + + setTitle(context.getText(R.string.dialog_title_external_access)); + setButton(context.getText(android.R.string.ok), this); + setButton2(context.getText(android.R.string.cancel), (OnClickListener) null); + setIcon(android.R.drawable.ic_dialog_alert); + + LayoutInflater inflater = + (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.dialog_allow_access, null); + setView(view); + + + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); + boolean externalAccess = sp.getBoolean(Preferences.PREFERENCE_ALLOW_EXTERNAL_ACCESS, false); + + mCheckBox = (CheckBox) view.findViewById(R.id.checkbox); + // mCheckBox.setText(R.string.pref_summary_external_access); + mCheckBox.setChecked(externalAccess); + + } + + + public void onClick(DialogInterface dialog, int which) { + if (which == BUTTON1) { + // User pressed OK + boolean externalAccess = mCheckBox.isChecked(); + + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); + SharedPreferences.Editor editor = sp.edit(); + editor.putBoolean(Preferences.PREFERENCE_ALLOW_EXTERNAL_ACCESS, externalAccess); + editor.commit(); + + } + + } + + + @Override + public Bundle onSaveInstanceState() { + Bundle state = super.onSaveInstanceState(); + state.putString(BUNDLE_TAGS, ""); + return state; + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + String tags = savedInstanceState.getString(BUNDLE_TAGS); + } +} diff --git a/src/org/openintents/safe/dialog/DialogHostingActivity.java b/src/org/openintents/safe/dialog/DialogHostingActivity.java index 04c8462..ff42d42 100644 --- a/src/org/openintents/safe/dialog/DialogHostingActivity.java +++ b/src/org/openintents/safe/dialog/DialogHostingActivity.java @@ -3,7 +3,6 @@ package org.openintents.safe.dialog; import org.openintents.distribution.GetFromMarketDialog; import org.openintents.distribution.RD; import org.openintents.intents.FileManagerIntents; -import org.openintents.safe.R; import org.openintents.util.IntentUtils; import android.app.Activity; @@ -22,6 +21,7 @@ public class DialogHostingActivity extends Activity { public static final int DIALOG_ID_SAVE = 1; public static final int DIALOG_ID_OPEN = 2; public static final int DIALOG_ID_NO_FILE_MANAGER_AVAILABLE = 3; + public static final int DIALOG_ID_ALLOW_EXTERNAL_ACCESS = 4; public static final String EXTRA_DIALOG_ID = "org.openintents.notepad.extra.dialog_id"; @@ -46,6 +46,9 @@ public class DialogHostingActivity extends Activity { case DIALOG_ID_NO_FILE_MANAGER_AVAILABLE: Log.i(TAG, "Show no file manager dialog"); showDialog(DIALOG_ID_NO_FILE_MANAGER_AVAILABLE); + case DIALOG_ID_ALLOW_EXTERNAL_ACCESS: + Log.i(TAG, "Show allow access dialog"); + showDialog(DIALOG_ID_ALLOW_EXTERNAL_ACCESS); break; } } @@ -110,7 +113,8 @@ public class DialogHostingActivity extends Activity { RD.string.filemanager_not_available, RD.string.filemanager_get_oi_filemanager, RD.string.filemanager_market_uri); - + case DIALOG_ID_ALLOW_EXTERNAL_ACCESS: + return new AllowExternalAccessDialog(this); } return null; } -- 2.50.1