From: David A. Velasco Date: Mon, 3 Feb 2014 13:24:28 +0000 (+0100) Subject: Added filtered chooser dialog for activities that can receive a link X-Git-Tag: oc-android-1.5.5~58^2~15^2 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/77d22d5a31d955a4b5b836cc4ee1d076944c7dd6?ds=inline Added filtered chooser dialog for activities that can receive a link --- diff --git a/res/layout-v11/activity_row.xml b/res/layout-v11/activity_row.xml new file mode 100644 index 00000000..95c7dfc5 --- /dev/null +++ b/res/layout-v11/activity_row.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/activity_row.xml b/res/layout/activity_row.xml new file mode 100644 index 00000000..b917600c --- /dev/null +++ b/res/layout/activity_row.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index fba0915c..5a61a15a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -187,7 +187,7 @@ Wait a moment "Unexpected problem ; please select the file from a different app" No file was selected - Send link to #8230; + Send link to … Login with oAuth2 Connecting to oAuth2 server… diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 4f42597f..07c5b536 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -18,9 +18,6 @@ package com.owncloud.android.ui.activity; -import java.util.ArrayList; -import java.util.List; - import org.apache.http.protocol.HTTP; import android.accounts.Account; @@ -28,12 +25,10 @@ import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.OperationCanceledException; +import android.support.v4.app.DialogFragment; import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; -import android.os.Parcelable; import android.webkit.MimeTypeMap; import android.widget.Toast; @@ -46,6 +41,7 @@ import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.accounts.OwnCloudAccount; import com.owncloud.android.lib.network.webdav.WebdavUtils; +import com.owncloud.android.ui.dialog.ActivityChooserDialog; import com.owncloud.android.utils.Log_OC; @@ -61,7 +57,9 @@ public abstract class FileActivity extends SherlockFragmentActivity { public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW"; public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION"; - public static final String TAG = FileActivity.class.getSimpleName(); + public static final String TAG = FileActivity.class.getSimpleName(); + + private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */ @@ -374,35 +372,17 @@ public abstract class FileActivity extends SherlockFragmentActivity { if (isSharedSupported()) { if (file != null) { - // Create the Share + // Create the Share - TODO integrate before or after the chooser menu //CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1); //createShare.execute(getStorageManager(), this, this, mHandler, this); - // TODO - // Get the link --> when the operation is finished - + // TODO Get the link --> when the operation is finished String link = "https://fake.url.lolo"; - Intent chooserIntent = null; - List targetedShareIntents = new ArrayList(); - List resInfo = getPackageManager().queryIntentActivities(createShareWithLinkIntent(link), PackageManager.MATCH_DEFAULT_ONLY); - String myPackageName = getPackageName(); - if (!resInfo.isEmpty()) { - for (ResolveInfo info : resInfo) { - if (!info.activityInfo.packageName.equalsIgnoreCase(myPackageName)) { - Intent targetedShare = createTargetedShare(link, info.activityInfo.applicationInfo.packageName, info.activityInfo.name); - targetedShareIntents.add(targetedShare); - } - } - } - if (targetedShareIntents.size() > 0) { - Intent firstTargeted = targetedShareIntents.remove(0); - chooserIntent = Intent.createChooser(firstTargeted, getString(R.string.action_share_file)); - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {})); - } else { - // to show standard message - chooserIntent = Intent.createChooser(null, getString(R.string.action_share_file)); - } - startActivity(chooserIntent); + + Intent intent = createShareWithLinkIntent(link); + String[] packagesToExclude = new String[] { getPackageName() }; + DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude); + chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); } else { Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); @@ -415,19 +395,6 @@ public abstract class FileActivity extends SherlockFragmentActivity { } } - private Intent createTargetedShare(String link, String packageName, String className) { - //Intent targetedShare = createShareWithLinkIntent(link); - Intent targetedShare=new Intent(Intent.ACTION_MAIN); - - targetedShare.addCategory(Intent.CATEGORY_LAUNCHER); - targetedShare.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); - Log_OC.e("LOLO", "className: " + className + "\npackageName: " + packageName + "\n"); - targetedShare.setClassName(packageName, className); - return targetedShare; - } - - private Intent createShareWithLinkIntent(String link) { Intent intentToShareLink = new Intent(Intent.ACTION_SEND); intentToShareLink.putExtra(Intent.EXTRA_TEXT, link); diff --git a/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java b/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java index fd910b40..133f46b9 100644 --- a/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java +++ b/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java @@ -17,26 +17,29 @@ package com.owncloud.android.ui.dialog; -import android.annotation.SuppressLint; -import android.app.Activity; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + import android.app.AlertDialog; import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.WindowManager.LayoutParams; import android.widget.ArrayAdapter; -import android.widget.EditText; +import android.widget.ImageView; import android.widget.ListAdapter; +import android.widget.TextView; import com.actionbarsherlock.app.SherlockDialogFragment; import com.owncloud.android.R; -import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision; -import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener; -import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.Log_OC; /** @@ -51,7 +54,7 @@ public class ActivityChooserDialog extends SherlockDialogFragment { private final static String ARG_INTENT = ActivityChooserDialog.class.getSimpleName() + ".ARG_INTENT"; private final static String ARG_PACKAGES_TO_EXCLUDE = ActivityChooserDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE"; - private ListAdapter mAdapter = null; //new ArrayAdapter(); + private ListAdapter mAdapter; public static ActivityChooserDialog newInstance(Intent intent, String[] packagesToExclude/*OnConflictDecisionMadeListener listener*/) { ActivityChooserDialog f = new ActivityChooserDialog(); @@ -70,12 +73,23 @@ public class ActivityChooserDialog extends SherlockDialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Intent intent = getArguments().getParcelable(ARG_INTENT); - String [] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE); + String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE); + List packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]); - // TODO init mAdapter + PackageManager pm= getSherlockActivity().getPackageManager(); + List activities = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); + Iterator it = activities.iterator(); + ResolveInfo resolveInfo; + while (it.hasNext()) { + resolveInfo = it.next(); + if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) { + it.remove(); + } + } + Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm)); + mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities); return new AlertDialog.Builder(getSherlockActivity()) - .setIcon(DisplayUtils.getSeasonalIconId()) .setTitle(R.string.activity_chooser_title) .setAdapter(mAdapter, new DialogInterface.OnClickListener() { @Override @@ -86,5 +100,40 @@ public class ActivityChooserDialog extends SherlockDialogFragment { }) .create(); } + + + class ActivityAdapter extends ArrayAdapter { + + private PackageManager mPackageManager; + + ActivityAdapter(Context context, PackageManager pm, List apps) { + //super(context, android.R.layout.activity_list_item, apps); + super(context, R.layout.activity_row, apps); + this.mPackageManager = pm; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = newView(parent); + } + bindView(position, convertView); + return convertView; + } + + private View newView(ViewGroup parent) { + return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false)); + //return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.activity_list_item, parent, false)); + } + + private void bindView(int position, View row) { + TextView label = (TextView) row.findViewById(R.id.title); + //TextView label = (TextView) row.findViewById(android.R.id.text1); + label.setText(getItem(position).loadLabel(mPackageManager)); + ImageView icon = (ImageView) row.findViewById(R.id.icon); + //ImageView icon = (ImageView) row.findViewById(android.R.id.icon); + icon.setImageDrawable(getItem(position).loadIcon(mPackageManager)); + } + } }