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