ShareLinkToDialog.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.ui.activity.ComponentsGetter;
  42. import com.owncloud.android.ui.activity.CopyToClipboardActivity;
  43. import com.owncloud.android.ui.activity.FileActivity;
  44. import com.owncloud.android.utils.Log_OC;
  45. /**
  46. * Dialog showing a list activities able to resolve a given Intent,
  47. * filtering out the activities matching give package names.
  48. *
  49. * @author David A. Velasco
  50. */
  51. public class ShareLinkToDialog extends SherlockDialogFragment {
  52. private final static String TAG = ShareLinkToDialog.class.getSimpleName();
  53. private final static String ARG_INTENT = ShareLinkToDialog.class.getSimpleName() + ".ARG_INTENT";
  54. private final static String ARG_PACKAGES_TO_EXCLUDE = ShareLinkToDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
  55. private final static String ARG_FILE_TO_SHARE = ShareLinkToDialog.class.getSimpleName() + ".FILE_TO_SHARE";
  56. private ActivityAdapter mAdapter;
  57. private OCFile mFile;
  58. private Intent mIntent;
  59. public static ShareLinkToDialog newInstance(Intent intent, String[] packagesToExclude, OCFile fileToShare) {
  60. ShareLinkToDialog f = new ShareLinkToDialog();
  61. Bundle args = new Bundle();
  62. args.putParcelable(ARG_INTENT, intent);
  63. args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude);
  64. args.putParcelable(ARG_FILE_TO_SHARE, fileToShare);
  65. f.setArguments(args);
  66. return f;
  67. }
  68. public ShareLinkToDialog() {
  69. super();
  70. Log_OC.d(TAG, "constructor");
  71. }
  72. @Override
  73. public Dialog onCreateDialog(Bundle savedInstanceState) {
  74. mIntent = getArguments().getParcelable(ARG_INTENT);
  75. String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE);
  76. List<String> packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]);
  77. mFile = getArguments().getParcelable(ARG_FILE_TO_SHARE);
  78. PackageManager pm= getSherlockActivity().getPackageManager();
  79. List<ResolveInfo> activities = pm.queryIntentActivities(mIntent, PackageManager.MATCH_DEFAULT_ONLY);
  80. Iterator<ResolveInfo> it = activities.iterator();
  81. ResolveInfo resolveInfo;
  82. while (it.hasNext()) {
  83. resolveInfo = it.next();
  84. if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) {
  85. it.remove();
  86. }
  87. }
  88. boolean sendAction = mIntent.getBooleanExtra(Intent.ACTION_SEND, false);
  89. if (!sendAction) {
  90. // add activity for copy to clipboard
  91. Intent copyToClipboardIntent = new Intent(getSherlockActivity(), CopyToClipboardActivity.class);
  92. List<ResolveInfo> copyToClipboard = pm.queryIntentActivities(copyToClipboardIntent, 0);
  93. if (!copyToClipboard.isEmpty()) {
  94. activities.add(copyToClipboard.get(0));
  95. }
  96. }
  97. Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
  98. mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
  99. return createSelector(sendAction);
  100. }
  101. private AlertDialog createSelector(final boolean sendAction) {
  102. int titleId;
  103. if (sendAction) {
  104. titleId = R.string.activity_chooser_send_file_title;
  105. } else {
  106. titleId = R.string.activity_chooser_title;
  107. }
  108. return new AlertDialog.Builder(getSherlockActivity())
  109. .setTitle(titleId)
  110. .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
  111. @Override
  112. public void onClick(DialogInterface dialog, int which) {
  113. // Add the information of the chosen activity to the intent to send
  114. ResolveInfo chosen = mAdapter.getItem(which);
  115. ActivityInfo actInfo = chosen.activityInfo;
  116. ComponentName name=new ComponentName(
  117. actInfo.applicationInfo.packageName,
  118. actInfo.name);
  119. mIntent.setComponent(name);
  120. if (sendAction) {
  121. dialog.dismiss(); // explicitly added for Android 2.x devices
  122. // Send the file
  123. ((FileActivity)getSherlockActivity()).startActivity(mIntent);
  124. } else {
  125. // Create a new share resource
  126. ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper()
  127. .shareFileWithLinkToApp(mFile, mIntent);
  128. }
  129. }
  130. })
  131. .create();
  132. }
  133. class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
  134. private PackageManager mPackageManager;
  135. ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
  136. super(context, R.layout.activity_row, apps);
  137. this.mPackageManager = pm;
  138. }
  139. @Override
  140. public View getView(int position, View convertView, ViewGroup parent) {
  141. if (convertView == null) {
  142. convertView = newView(parent);
  143. }
  144. bindView(position, convertView);
  145. return convertView;
  146. }
  147. private View newView(ViewGroup parent) {
  148. return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
  149. }
  150. private void bindView(int position, View row) {
  151. TextView label = (TextView) row.findViewById(R.id.title);
  152. label.setText(getItem(position).loadLabel(mPackageManager));
  153. ImageView icon = (ImageView) row.findViewById(R.id.icon);
  154. icon.setImageDrawable(getItem(position).loadIcon(mPackageManager));
  155. }
  156. }
  157. }