ActivityChooserDialog.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2014 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.ui.dialog;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import android.app.AlertDialog;
  23. import android.app.Dialog;
  24. import android.content.ComponentName;
  25. import android.content.Context;
  26. import android.content.DialogInterface;
  27. import android.content.Intent;
  28. import android.content.pm.ActivityInfo;
  29. import android.content.pm.PackageManager;
  30. import android.content.pm.ResolveInfo;
  31. import android.os.Bundle;
  32. import android.view.LayoutInflater;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.widget.ArrayAdapter;
  36. import android.widget.ImageView;
  37. import android.widget.TextView;
  38. import com.actionbarsherlock.app.SherlockDialogFragment;
  39. import com.owncloud.android.R;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.files.FileOperationsHelper;
  42. import com.owncloud.android.ui.activity.FileActivity;
  43. import com.owncloud.android.utils.Log_OC;
  44. /**
  45. * Dialog showing a list activities able to resolve a given Intent,
  46. * filtering out the activities matching give package names.
  47. *
  48. * @author David A. Velasco
  49. */
  50. public class ActivityChooserDialog extends SherlockDialogFragment {
  51. private final static String TAG = ActivityChooserDialog.class.getSimpleName();
  52. private final static String ARG_INTENT = ActivityChooserDialog.class.getSimpleName() + ".ARG_INTENT";
  53. private final static String ARG_PACKAGES_TO_EXCLUDE = ActivityChooserDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
  54. private final static String ARG_FILE_TO_SHARE = ActivityChooserDialog.class.getSimpleName() + ".FILE_TO_SHARE";
  55. private ActivityAdapter mAdapter;
  56. private OCFile mFile;
  57. private Intent mIntent;
  58. public static ActivityChooserDialog newInstance(Intent intent, String[] packagesToExclude, OCFile fileToShare) {
  59. ActivityChooserDialog f = new ActivityChooserDialog();
  60. Bundle args = new Bundle();
  61. args.putParcelable(ARG_INTENT, intent);
  62. args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude);
  63. args.putParcelable(ARG_FILE_TO_SHARE, fileToShare);
  64. f.setArguments(args);
  65. return f;
  66. }
  67. public ActivityChooserDialog() {
  68. super();
  69. Log_OC.d(TAG, "constructor");
  70. }
  71. @Override
  72. public Dialog onCreateDialog(Bundle savedInstanceState) {
  73. mIntent = getArguments().getParcelable(ARG_INTENT);
  74. String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE);
  75. List<String> packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]);
  76. mFile = getArguments().getParcelable(ARG_FILE_TO_SHARE);
  77. PackageManager pm= getSherlockActivity().getPackageManager();
  78. List<ResolveInfo> activities = pm.queryIntentActivities(mIntent, PackageManager.MATCH_DEFAULT_ONLY);
  79. Iterator<ResolveInfo> it = activities.iterator();
  80. ResolveInfo resolveInfo;
  81. while (it.hasNext()) {
  82. resolveInfo = it.next();
  83. if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) {
  84. it.remove();
  85. }
  86. }
  87. Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
  88. mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
  89. boolean sendAction = mIntent.getBooleanExtra(Intent.ACTION_SEND, false);
  90. if (sendAction) {
  91. return new AlertDialog.Builder(getSherlockActivity())
  92. .setTitle(R.string.activity_chooser_title)
  93. .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
  94. @Override
  95. public void onClick(DialogInterface dialog, int which) {
  96. // Add the information of the chosen activity to the intent to send
  97. ResolveInfo chosen = mAdapter.getItem(which);
  98. ActivityInfo actInfo = chosen.activityInfo;
  99. ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
  100. mIntent.setComponent(name);
  101. // Send the file
  102. ((FileActivity)getSherlockActivity()).startActivity(mIntent);
  103. }
  104. })
  105. .create();
  106. } else {
  107. return new AlertDialog.Builder(getSherlockActivity())
  108. .setTitle(R.string.activity_chooser_send_file_title)
  109. .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
  110. @Override
  111. public void onClick(DialogInterface dialog, int which) {
  112. // Add the information of the chosen activity to the intent to send
  113. ResolveInfo chosen = mAdapter.getItem(which);
  114. ActivityInfo actInfo = chosen.activityInfo;
  115. ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
  116. mIntent.setComponent(name);
  117. // Create a new share resource
  118. FileOperationsHelper foh = new FileOperationsHelper();
  119. foh.shareFileWithLinkToApp(mFile, mIntent, (FileActivity)getSherlockActivity());
  120. }
  121. })
  122. .create();
  123. }
  124. }
  125. class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
  126. private PackageManager mPackageManager;
  127. ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
  128. super(context, R.layout.activity_row, apps);
  129. this.mPackageManager = pm;
  130. }
  131. @Override
  132. public View getView(int position, View convertView, ViewGroup parent) {
  133. if (convertView == null) {
  134. convertView = newView(parent);
  135. }
  136. bindView(position, convertView);
  137. return convertView;
  138. }
  139. private View newView(ViewGroup parent) {
  140. return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
  141. }
  142. private void bindView(int position, View row) {
  143. TextView label = (TextView) row.findViewById(R.id.title);
  144. label.setText(getItem(position).loadLabel(mPackageManager));
  145. ImageView icon = (ImageView) row.findViewById(R.id.icon);
  146. icon.setImageDrawable(getItem(position).loadIcon(mPackageManager));
  147. }
  148. }
  149. }