import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
-import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
-import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import com.cyanogenmod.filemanager.R;
+import com.cyanogenmod.filemanager.activities.EditorActivity;
import com.cyanogenmod.filemanager.activities.ShortcutActivity;
import com.cyanogenmod.filemanager.console.secure.SecureConsole;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.providers.secure.ISecureChoiceCompleteListener;
import com.cyanogenmod.filemanager.providers.secure.SecureCacheCleanupService;
import com.cyanogenmod.filemanager.providers.secure.SecureChoiceClickListener;
-import com.cyanogenmod.filemanager.ui.dialogs.AssociationsDialog;
import com.cyanogenmod.filemanager.util.DialogHelper;
import com.cyanogenmod.filemanager.util.ExceptionUtil;
import com.cyanogenmod.filemanager.util.FileHelper;
* @param ctx The current context
* @param fso The file system object
* @param choose If allow the user to select the application to open with
- * @param onCancelListener The cancel listener
* @param onDismissListener The dismiss listener
*/
public static void openFileSystemObject(
final Context ctx, final FileSystemObject fso, final boolean choose,
- final OnCancelListener onCancelListener, final OnDismissListener onDismissListener) {
+ final OnDismissListener onDismissListener) {
try {
// Create the intent to open the file
final Intent intent = new Intent();
ctx,
intent,
choose,
- createInternalIntents(ctx, cacheFso),
- 0,
- R.string.associations_dialog_openwith_title,
- R.string.associations_dialog_openwith_action,
- true,
- onCancelListener,
onDismissListener);
}
}
// Resolve the intent
- resolveIntent(
- ctx,
- intent,
- choose,
- createInternalIntents(ctx, fso),
- 0,
- R.string.associations_dialog_openwith_title,
- R.string.associations_dialog_openwith_action,
- true, onCancelListener, onDismissListener);
-
+ resolveIntent(ctx, intent, choose, onDismissListener);
} catch (Exception e) {
ExceptionUtil.translateException(ctx, e);
}
}
+ private static boolean handledByEditorInManifest(Context context, Intent intent) {
+ Intent i = new Intent(intent);
+ i.setPackage(context.getPackageName());
+ return context.getPackageManager().queryIntentActivities(i, 0).size() > 0;
+ }
+
/**
* Method that sends a {@link FileSystemObject} with the default registered application
* by the system, or ask the user for select a registered application.
*
* @param ctx The current context
* @param fso The file system object
- * @param onCancelListener The cancel listener
* @param onDismissListener The dismiss listener
*/
public static void sendFileSystemObject(
- final Context ctx, final FileSystemObject fso,
- OnCancelListener onCancelListener, OnDismissListener onDismissListener) {
+ final Context ctx, final FileSystemObject fso, OnDismissListener onDismissListener) {
try {
// Create the intent to
Intent intent = new Intent();
resolveIntent(
ctx,
intent,
- true,
- null,
- 0,
- R.string.associations_dialog_sendwith_title,
- R.string.associations_dialog_sendwith_action,
- false, onCancelListener, onDismissListener);
+ false,
+ onDismissListener);
} catch (Exception e) {
ExceptionUtil.translateException(ctx, e);
*
* @param ctx The current context
* @param fsos The file system objects
- * @param onCancelListener The cancel listener
* @param onDismissListener The dismiss listener
*/
public static void sendMultipleFileSystemObject(
final Context ctx, final List<FileSystemObject> fsos,
- OnCancelListener onCancelListener, OnDismissListener onDismissListener) {
+ OnDismissListener onDismissListener) {
try {
// Create the intent to
Intent intent = new Intent();
resolveIntent(
ctx,
intent,
- true,
- null,
- 0,
- R.string.associations_dialog_sendwith_title,
- R.string.associations_dialog_sendwith_action,
- false, onCancelListener, onDismissListener);
+ false,
+ onDismissListener);
} catch (Exception e) {
ExceptionUtil.translateException(ctx, e);
*
* @param ctx The current context
* @param intent The intent to resolve
- * @param choose If allow the user to select the application to select the registered
* application. If no preferred app or more than one exists the dialog is shown.
- * @param internals The list of internals intents that can handle the action
- * @param icon The icon of the dialog
- * @param title The title of the dialog
- * @param action The button title of the dialog
- * @param allowPreferred If allow the user to mark the selected app as preferred
- * @param onCancelListener The cancel listener
* @param onDismissListener The dismiss listener
*/
private static void resolveIntent(
- Context ctx, Intent intent, boolean choose, List<Intent> internals,
- int icon, int title, int action, boolean allowPreferred,
- OnCancelListener onCancelListener, OnDismissListener onDismissListener) {
- //Retrieve the activities that can handle the file
- final PackageManager packageManager = ctx.getPackageManager();
- if (DEBUG) {
- intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
- }
- List<ResolveInfo> info =
- packageManager.
- queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
- Collections.sort(info, new Comparator<ResolveInfo>() {
- @Override
- public int compare(ResolveInfo lhs, ResolveInfo rhs) {
- boolean isLshCMFM =
- lhs.activityInfo.packageName.compareTo(PREFERRED_PACKAGE) == 0;
- boolean isRshCMFM =
- rhs.activityInfo.packageName.compareTo(PREFERRED_PACKAGE) == 0;
- if (isLshCMFM && !isRshCMFM) {
- return -1;
+ Context ctx, Intent intent, boolean choose, OnDismissListener onDismissListener) {
+ if (choose) {
+ PackageManager pm = ctx.getPackageManager();
+ List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
+
+ Intent editor = new Intent(intent);
+ editor.setClass(ctx, EditorActivity.class);
+
+ if (infos.size() > 0) {
+ // Try to only show the chooser when we have multiple items
+ Intent i = Intent.createChooser(intent,
+ ctx.getString(R.string.associations_dialog_openwith_title));
+ if (!handledByEditorInManifest(ctx, intent)) {
+ i.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{
+ editor,
+ });
}
- if (!isLshCMFM && isRshCMFM) {
- return 1;
- }
- return lhs.activityInfo.name.compareTo(rhs.activityInfo.name);
- }
- });
-
- if (info.size() == 0) {
- // This basically checks the system to see if it possibly wants to handle it
- ResolveInfo ri = packageManager.resolveActivity(intent, 0);
- if (ri != null) {
try {
- ctx.startActivity(getIntentFromResolveInfo(ri, intent));
- if (onDismissListener != null) {
- onDismissListener.onDismiss(null);
- }
- return;
+ ctx.startActivity(i);
} catch (ActivityNotFoundException e) {
- // fall through, we may still want to handle it with an internal activity
- }
- }
- }
-
- // Add the internal editors
- int count = 0;
- if (internals != null) {
- int cc = internals.size();
- for (int i = 0; i < cc; i++) {
- Intent ii = internals.get(i);
- List<ResolveInfo> ie =
- packageManager.
- queryIntentActivities(ii, 0);
- if (ie.size() > 0) {
- ResolveInfo rie = ie.get(0);
-
- // Only if the internal is not in the query list
- boolean exists = false;
- int ccc = info.size();
- for (int j = 0; j < ccc; j++) {
- ResolveInfo ri = info.get(j);
- if (ri.activityInfo.packageName.compareTo(
- rie.activityInfo.packageName) == 0 &&
- ri.activityInfo.name.compareTo(
- rie.activityInfo.name) == 0) {
-
- // Mark as internal
- if (ri.activityInfo.metaData == null) {
- ri.activityInfo.metaData = new Bundle();
- ri.activityInfo.metaData.putString(
- EXTRA_INTERNAL_ACTION, ii.getAction());
- ri.activityInfo.metaData.putBoolean(
- CATEGORY_INTERNAL_VIEWER, true);
- }
- exists = true;
- break;
- }
- }
- if (exists) {
- continue;
- }
-
- // Mark as internal
- if (rie.activityInfo.metaData == null) {
- rie.activityInfo.metaData = new Bundle();
- rie.activityInfo.metaData.putString(EXTRA_INTERNAL_ACTION, ii.getAction());
- rie.activityInfo.metaData.putBoolean(CATEGORY_INTERNAL_VIEWER, true);
+ try {
+ ctx.startActivity(editor);
+ } catch (ActivityNotFoundException e1) {
+ // Do nothing, this should never happen
}
-
- // Only one result must be matched
- info.add(count, rie);
- count++;
}
- }
- }
-
- // No registered application
- if (info.size() == 0) {
- DialogHelper.showToast(ctx, R.string.msgs_not_registered_app, Toast.LENGTH_SHORT);
- if (onDismissListener != null) {
- onDismissListener.onDismiss(null);
- }
- return;
- }
-
- // Retrieve the preferred activity that can handle the file. We only want the
- // resolved activity if the activity is a preferred activity. Other case, the
- // resolved activity was never added by addPreferredActivity
- ResolveInfo mPreferredInfo = findPreferredActivity(ctx, intent, info);
-
- // Is a simple open and we have an application that can handle the file?
- //---
- // If we have a preferred application, then use it
- if (!choose && (mPreferredInfo != null && mPreferredInfo.match != 0)) {
- try {
- ctx.startActivity(getIntentFromResolveInfo(mPreferredInfo, intent));
- if (onDismissListener != null) {
- onDismissListener.onDismiss(null);
+ } else {
+ try {
+ ctx.startActivity(editor);
+ } catch (ActivityNotFoundException e) {
+ // Do nothing, this should never happen
}
- return;
- } catch (ActivityNotFoundException e) {
- // fall through, the preferred may have been uninstalled.
}
- }
- // If there are only one activity (app or internal editor), then use it
- if (!choose && info.size() == 1) {
- ResolveInfo ri = info.get(0);
+ } else {
try {
- ctx.startActivity(getIntentFromResolveInfo(ri, intent));
+ ctx.startActivity(intent);
} catch (ActivityNotFoundException e) {
- DialogHelper.showToast(ctx, R.string.msgs_not_registered_app, Toast.LENGTH_SHORT);
- }
- if (onDismissListener != null) {
- onDismissListener.onDismiss(null);
+ intent.setClass(ctx, EditorActivity.class);
+ try {
+ ctx.startActivity(intent);
+ } catch (ActivityNotFoundException e2) {
+ // This should never happen unless the editor is removed.
+ }
}
- return;
}
-
- // If we have multiples apps and there is not a preferred application then show
- // open with dialog
- AssociationsDialog dialog =
- new AssociationsDialog(
- ctx,
- icon,
- ctx.getString(title),
- ctx.getString(action),
- intent,
- info,
- mPreferredInfo,
- allowPreferred,
- onCancelListener,
- onDismissListener);
- dialog.show();
+ if (onDismissListener != null) {
+ onDismissListener.onDismiss(null);
+ }
}
/**
*/
private static List<Intent> createInternalIntents(Context ctx, FileSystemObject fso) {
List<Intent> intents = new ArrayList<Intent>();
- intents.addAll(createEditorIntent(ctx, fso));
+ intents.addAll(createEditorIntents(ctx, fso));
return intents;
}
* @param ctx The current context
* @param fso FileSystemObject
*/
- private static List<Intent> createEditorIntent(Context ctx, FileSystemObject fso) {
+ private static List<Intent> createEditorIntents(Context ctx, FileSystemObject fso) {
List<Intent> intents = new ArrayList<Intent>();
MimeTypeCategory category = MimeTypeHelper.getCategory(ctx, fso);
return intents;
}
+ private static Intent createEditorIntent(Context ctx, FileSystemObject fso) {
+ Intent editorIntent = null;
+ MimeTypeCategory category = MimeTypeHelper.getCategory(ctx, fso);
+
+ editorIntent = new Intent();
+ editorIntent.setAction(Intent.ACTION_VIEW);
+ editorIntent.addCategory(CATEGORY_INTERNAL_VIEWER);
+ editorIntent.addCategory(CATEGORY_EDITOR);
+ return editorIntent;
+ }
+
/**
* Method that returns an {@link Intent} from his {@link ResolveInfo}
*