Skip to content

Commit

Permalink
CMFileManager: AOSP GET_CONTENT_DATA compatibility
Browse files Browse the repository at this point in the history
This change brings compatibility to GET_CONTENT_DATA for AOSP apps when
using the PickerActivity:

* Detect crop extra; use the com.android.camera.action.CROP action of Gallery3d
to crop and return the requested image. This gets compatilibity for example with
the contacts app, when a user try to set the image of a contact.
* Detect android.provider.MediaStore.Audio.Media.EXTRA_MAX_BYTES; when this extra
is present the PickerActivity only display (and allow select) files with a size lower
than requested.
* Detect Intent.EXTRA_LOCAL_ONLY; useless until CMFM allow access remote file systems.

Change-Id: I1020458505b236653e869ec1c1f532dd6d686633
  • Loading branch information
jruesga committed Nov 17, 2012
1 parent a0eb955 commit 3183792
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 31 deletions.
107 changes: 101 additions & 6 deletions src/com/cyanogenmod/filemanager/activities/PickerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
Expand All @@ -45,6 +46,7 @@
import com.cyanogenmod.filemanager.adapters.CheckableListAdapter.CheckableItem;
import com.cyanogenmod.filemanager.console.ConsoleBuilder;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.preferences.DisplayRestrictions;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.Preferences;
import com.cyanogenmod.filemanager.ui.ThemeManager;
Expand All @@ -60,7 +62,9 @@

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The activity for allow to use a {@link NavigationView} like, to pick a file from other
Expand All @@ -84,7 +88,22 @@ public void onReceive(Context context, Intent intent) {
}
};

private String mMimeType;
// The result code
private static final int RESULT_CROP_IMAGE = 1;

// The component that holds the crop operation. We use Gallery3d because we are confidence
// of his input parameters
private static final ComponentName CROP_COMPONENT =
new ComponentName(
"com.android.gallery3d", //$NON-NLS-1$
"com.android.gallery3d.app.CropImage"); //$NON-NLS-1$

// Gallery crop editor action
private static final String ACTION_CROP = "com.android.camera.action.CROP"; //$NON-NLS-1$

// Extra data for Gallery CROP action
private static final String EXTRA_CROP = "crop"; //$NON-NLS-1$

private FileSystemObject mFso; // The picked item
private AlertDialog mDialog;
private Handler mHandler;
Expand Down Expand Up @@ -151,14 +170,38 @@ public void onConfigurationChanged(Configuration newConfig) {
private void init() {
// Check that call has a valid request (GET_CONTENT a and mime type)
String action = getIntent().getAction();
this.mMimeType = getIntent().getType();
if (action.compareTo(Intent.ACTION_GET_CONTENT.toString()) != 0 ||
this.mMimeType == null) {

if (action.compareTo(Intent.ACTION_GET_CONTENT.toString()) != 0) {
setResult(Activity.RESULT_CANCELED);
finish();
return;
}

// Display restrictions
Map<DisplayRestrictions, Object> restrictions = new HashMap<DisplayRestrictions, Object>();
//- Mime/Type restriction
String mimeType = getIntent().getType();
if (mimeType != null) {
restrictions.put(DisplayRestrictions.MIME_TYPE_RESTRICTION, mimeType);
}
// Other restrictions
Bundle extras = getIntent().getExtras();
if (extras != null) {
//-- File size
if (extras.containsKey(android.provider.MediaStore.Audio.Media.EXTRA_MAX_BYTES)) {
long size =
extras.getLong(android.provider.MediaStore.Audio.Media.EXTRA_MAX_BYTES);
restrictions.put(DisplayRestrictions.SIZE_RESTRICTION, Long.valueOf(size));
}
//-- Local filesystems only
if (extras.containsKey(Intent.EXTRA_LOCAL_ONLY)) {
boolean localOnly = extras.getBoolean(Intent.EXTRA_LOCAL_ONLY);
restrictions.put(
DisplayRestrictions.LOCAL_FILESYSTEM_ONLY_RESTRICTION,
Boolean.valueOf(localOnly));
}
}

// Create or use the console
if (!initializeConsole()) {
// Something when wrong. Display a message and exit
Expand Down Expand Up @@ -187,7 +230,7 @@ public void run() {
// Navigation view
this.mNavigationView =
(NavigationView)this.mRootView.findViewById(R.id.navigation_view);
this.mNavigationView.setMimeType(this.mMimeType);
this.mNavigationView.setRestrictions(restrictions);
this.mNavigationView.setOnFilePickedListener(this);
this.mNavigationView.setBreadcrumb(breadcrumb);

Expand Down Expand Up @@ -265,16 +308,68 @@ private boolean initializeConsole() {
return false;
}

/**
* {@inheritDoc}
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_CROP_IMAGE:
// Return what the callee activity returns
setResult(resultCode, data);
finish();
return;

default:
break;
}

// The response is not understood
Log.w(TAG,
String.format(
"Ignore response. requestCode: %s, resultCode: %s, data: %s", //$NON-NLS-1$
Integer.valueOf(requestCode),
Integer.valueOf(resultCode),
data));
DialogHelper.showToast(this, R.string.msgs_operation_failure, Toast.LENGTH_SHORT);
}

/**
* {@inheritDoc}
*/
@Override
public void onDismiss(DialogInterface dialog) {
if (this.mFso != null) {
File src = new File(this.mFso.getFullPath());
if (getIntent().getExtras() != null) {
// Some AOSP applications use the gallery to edit and crop the selected image
// with the Gallery crop editor. In this case pass the picked file to the
// CropActivity with the requested parameters
// Expected result is on onActivityResult
Bundle extras = getIntent().getExtras();
String crop = extras.getString(EXTRA_CROP);
if (Boolean.parseBoolean(crop)) {
// We want to use the Gallery3d activity because we know about it, and his
// parameters. At least we have a compatible one.
Intent intent = new Intent(ACTION_CROP);
if (getIntent().getType() != null) {
intent.setType(getIntent().getType());
}
intent.setData(Uri.fromFile(src));
intent.putExtras(extras);
intent.setComponent(CROP_COMPONENT);
startActivityForResult(intent, RESULT_CROP_IMAGE);
return;
}
}

// Return the picked file, as expected (this activity should fill the intent data
// and return RESULT_OK result)
Intent result = new Intent();
result.setData(Uri.fromFile(new File(this.mFso.getFullPath())));
result.setData(Uri.fromFile(src));
setResult(Activity.RESULT_OK, result);
finish();

} else {
cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyanogenmod.filemanager.preferences;

/**
* An enumeration of the restrictions that can be applied when displaying list of files.
*/
public enum DisplayRestrictions {
/**
* Restriction for display only files with the category.
*/
CATEGORY_TYPE_RESTRICTION,
/**
* Restriction for display only files with the mime/type.
*/
MIME_TYPE_RESTRICTION,
/**
* Restriction for display only files with a size lower than the specified
*/
SIZE_RESTRICTION,
/**
* Restriction for display only files from the local file system. Avoid remote files.
*/
LOCAL_FILESYSTEM_ONLY_RESTRICTION
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.cyanogenmod.filemanager.model.Query;
import com.cyanogenmod.filemanager.model.SearchResult;
import com.cyanogenmod.filemanager.preferences.AccessMode;
import com.cyanogenmod.filemanager.preferences.DisplayRestrictions;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.NavigationSortMode;
import com.cyanogenmod.filemanager.preferences.ObjectStringIdentifier;
Expand All @@ -40,7 +41,9 @@

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* A class for paint the resulting file system object of a search.
Expand Down Expand Up @@ -112,11 +115,17 @@ protected Boolean doInBackground(Object... params) {
boolean chRooted =
FileManagerApplication.getAccessMode().compareTo(AccessMode.SAFE) == 0;

// Create display restrictions
Map<DisplayRestrictions, Object> restrictions =
new HashMap<DisplayRestrictions, Object>();
restrictions.put(
DisplayRestrictions.MIME_TYPE_RESTRICTION, MimeTypeHelper.ALL_MIME_TYPES);

//Process all the data
final List<SearchResult> result =
SearchHelper.convertToResults(
FileHelper.applyUserPreferences(
this.mFiles, MimeTypeHelper.ALL_MIME_TYPES, true, chRooted),
this.mFiles, restrictions, true, chRooted),
this.mQueries);
if (mode.compareTo(SearchSortResultMode.NAME) == 0) {
Collections.sort(result, new Comparator<SearchResult>() {
Expand Down
30 changes: 17 additions & 13 deletions src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.cyanogenmod.filemanager.parcelables.NavigationViewInfoParcelable;
import com.cyanogenmod.filemanager.parcelables.SearchInfoParcelable;
import com.cyanogenmod.filemanager.preferences.AccessMode;
import com.cyanogenmod.filemanager.preferences.DisplayRestrictions;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.NavigationLayoutMode;
import com.cyanogenmod.filemanager.preferences.ObjectIdentifier;
Expand All @@ -60,11 +61,12 @@
import com.cyanogenmod.filemanager.util.DialogHelper;
import com.cyanogenmod.filemanager.util.ExceptionUtil;
import com.cyanogenmod.filemanager.util.FileHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper;
import com.cyanogenmod.filemanager.util.StorageHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The file manager implementation view (contains the graphical representation and the input
Expand Down Expand Up @@ -204,7 +206,8 @@ public void onItemFlingerEnd(OnItemFlingerResponder responder,

private NAVIGATION_MODE mNavigationMode;

private String mMimeType = MimeTypeHelper.ALL_MIME_TYPES;
// Restrictions
private Map<DisplayRestrictions, Object> mRestrictions;

/**
* @hide
Expand Down Expand Up @@ -318,6 +321,9 @@ private void init(TypedArray tarray) {
this.mNavigationMode = NAVIGATION_MODE.values()[mode];
}

// Initialize default restrictions (no restrictions)
this.mRestrictions = new HashMap<DisplayRestrictions, Object>();

//Initialize variables
this.mFiles = new ArrayList<FileSystemObject>();

Expand Down Expand Up @@ -345,23 +351,21 @@ private void init(TypedArray tarray) {
}

/**
* Method that returns the mime/type used by this class. Only the files with this mime/type
* are shown.
* Method that returns the display restrictions to apply to this view.
*
* @return String The mime/type
* @return Map<DisplayRestrictions, Object> The restrictions to apply
*/
public String getMimeType() {
return this.mMimeType;
public Map<DisplayRestrictions, Object> getRestrictions() {
return this.mRestrictions;
}

/**
* Method that sets the mime/type used by this class. Only the files with this mime/type
* are shown.
* Method that sets the display restrictions to apply to this view.
*
* @param mimeType String The mime/type
* @param mRestrictions The restrictions to apply
*/
public void setMimeType(String mimeType) {
this.mMimeType = mimeType;
public void setRestrictions(Map<DisplayRestrictions, Object> mRestrictions) {
this.mRestrictions = mRestrictions;
}

/**
Expand Down Expand Up @@ -888,7 +892,7 @@ void onPostExecuteTask(

//Apply user preferences
List<FileSystemObject> sortedFiles =
FileHelper.applyUserPreferences(files, this.mMimeType, this.mChRooted);
FileHelper.applyUserPreferences(files, this.mRestrictions, this.mChRooted);

//Remove parent directory if we are in the root of a chrooted environment
if (this.mChRooted && StorageHelper.isStorageVolume(newDir)) {
Expand Down
Loading

0 comments on commit 3183792

Please sign in to comment.