Merge branch 'develop' into send_file_pr311_with_develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / ActivityChooserDialog.java
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
18 package com.owncloud.android.ui.dialog;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.pm.ActivityInfo;
32 import android.content.pm.PackageManager;
33 import android.content.pm.ResolveInfo;
34 import android.os.Bundle;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ArrayAdapter;
39 import android.widget.ImageView;
40 import android.widget.TextView;
41
42 import com.actionbarsherlock.app.SherlockDialogFragment;
43 import com.owncloud.android.R;
44 import com.owncloud.android.datamodel.OCFile;
45 import com.owncloud.android.files.FileOperationsHelper;
46 import com.owncloud.android.ui.activity.FileActivity;
47 import com.owncloud.android.utils.Log_OC;
48
49 /**
50 * Dialog showing a list activities able to resolve a given Intent,
51 * filtering out the activities matching give package names.
52 *
53 * @author David A. Velasco
54 */
55 public class ActivityChooserDialog extends SherlockDialogFragment {
56
57 private final static String TAG = ActivityChooserDialog.class.getSimpleName();
58 private final static String ARG_INTENT = ActivityChooserDialog.class.getSimpleName() + ".ARG_INTENT";
59 private final static String ARG_PACKAGES_TO_EXCLUDE = ActivityChooserDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
60 private final static String ARG_FILE_TO_SHARE = ActivityChooserDialog.class.getSimpleName() + ".FILE_TO_SHARE";
61
62 private ActivityAdapter mAdapter;
63 private OCFile mFile;
64 private Intent mIntent;
65
66 public static ActivityChooserDialog newInstance(Intent intent, String[] packagesToExclude, OCFile fileToShare) {
67 ActivityChooserDialog f = new ActivityChooserDialog();
68 Bundle args = new Bundle();
69 args.putParcelable(ARG_INTENT, intent);
70 args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude);
71 args.putParcelable(ARG_FILE_TO_SHARE, fileToShare);
72 f.setArguments(args);
73 return f;
74 }
75
76 public ActivityChooserDialog() {
77 super();
78 Log_OC.d(TAG, "constructor");
79 }
80
81 @Override
82 public Dialog onCreateDialog(Bundle savedInstanceState) {
83 mIntent = getArguments().getParcelable(ARG_INTENT);
84 String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE);
85 List<String> packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]);
86 mFile = getArguments().getParcelable(ARG_FILE_TO_SHARE);
87
88 PackageManager pm= getSherlockActivity().getPackageManager();
89 List<ResolveInfo> activities = pm.queryIntentActivities(mIntent, PackageManager.MATCH_DEFAULT_ONLY);
90 Iterator<ResolveInfo> it = activities.iterator();
91 ResolveInfo resolveInfo;
92 while (it.hasNext()) {
93 resolveInfo = it.next();
94 if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) {
95 it.remove();
96 }
97 }
98 Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
99 mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
100
101 boolean sendAction = mIntent.getBooleanExtra(Intent.ACTION_SEND, false);
102
103 if (sendAction) {
104
105 return new AlertDialog.Builder(getSherlockActivity())
106 .setTitle(R.string.activity_chooser_title)
107 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
108 @Override
109 public void onClick(DialogInterface dialog, int which) {
110 // Add the information of the chosen activity to the intent to send
111 ResolveInfo chosen = mAdapter.getItem(which);
112 ActivityInfo actInfo = chosen.activityInfo;
113 ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
114 mIntent.setComponent(name);
115
116 // Send the file
117 ((FileActivity)getSherlockActivity()).startActivity(mIntent);
118
119 }
120 })
121 .create();
122 } else {
123 return new AlertDialog.Builder(getSherlockActivity())
124 .setTitle(R.string.activity_chooser_send_file_title)
125 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
126 @Override
127 public void onClick(DialogInterface dialog, int which) {
128 // Add the information of the chosen activity to the intent to send
129 ResolveInfo chosen = mAdapter.getItem(which);
130 ActivityInfo actInfo = chosen.activityInfo;
131 ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
132 mIntent.setComponent(name);
133
134 // Create a new share resource
135 FileOperationsHelper foh = new FileOperationsHelper();
136 foh.shareFileWithLinkToApp(mFile, mIntent, (FileActivity)getSherlockActivity());
137 }
138 })
139 .create();
140 }
141 }
142
143
144 class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
145
146 private PackageManager mPackageManager;
147
148 ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
149 super(context, R.layout.activity_row, apps);
150 this.mPackageManager = pm;
151 }
152
153 @Override
154 public View getView(int position, View convertView, ViewGroup parent) {
155 if (convertView == null) {
156 convertView = newView(parent);
157 }
158 bindView(position, convertView);
159 return convertView;
160 }
161
162 private View newView(ViewGroup parent) {
163 return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
164 }
165
166 private void bindView(int position, View row) {
167 TextView label = (TextView) row.findViewById(R.id.title);
168 label.setText(getItem(position).loadLabel(mPackageManager));
169 ImageView icon = (ImageView) row.findViewById(R.id.icon);
170 icon.setImageDrawable(getItem(position).loadIcon(mPackageManager));
171 }
172 }
173
174 }