b651d33dcd35acd7e4954eb9705f9d0445465f55
[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 if (sendAction) {
115 return new AlertDialog.Builder(getSherlockActivity())
116 .setTitle(R.string.activity_chooser_send_file_title)
117 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
118 @Override
119 public void onClick(DialogInterface dialog, int which) {
120 // Add the information of the chosen activity to the intent to send
121 ResolveInfo chosen = mAdapter.getItem(which);
122 ActivityInfo actInfo = chosen.activityInfo;
123 ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
124 mIntent.setComponent(name);
125
126 // Send the file
127 FileOperationsHelper foh = new FileOperationsHelper();
128 foh.sendFileToApp(mIntent, (FileActivity)getSherlockActivity());
129
130 }
131 })
132 .create();
133
134 } else {
135 return new AlertDialog.Builder(getSherlockActivity())
136 .setTitle(R.string.activity_chooser_title)
137 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
138 @Override
139 public void onClick(DialogInterface dialog, int which) {
140 // Add the information of the chosen activity to the intent to send
141 ResolveInfo chosen = mAdapter.getItem(which);
142 ActivityInfo actInfo = chosen.activityInfo;
143 ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name);
144 mIntent.setComponent(name);
145
146 // Create a new share resource
147 FileOperationsHelper foh = new FileOperationsHelper();
148 foh.shareFileWithLinkToApp(mFile, mIntent, (FileActivity)getSherlockActivity());
149 }
150 })
151 .create();
152 }
153 }
154
155
156 class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
157
158 private PackageManager mPackageManager;
159
160 ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
161 super(context, R.layout.activity_row, apps);
162 this.mPackageManager = pm;
163 }
164
165 @Override
166 public View getView(int position, View convertView, ViewGroup parent) {
167 if (convertView == null) {
168 convertView = newView(parent);
169 }
170 bindView(position, convertView);
171 return convertView;
172 }
173
174 private View newView(ViewGroup parent) {
175 return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false));
176 }
177
178 private void bindView(int position, View row) {
179 TextView label = (TextView) row.findViewById(R.id.title);
180 label.setText(getItem(position).loadLabel(mPackageManager));
181 ImageView icon = (ImageView) row.findViewById(R.id.icon);
182 icon.setImageDrawable(getItem(position).loadIcon(mPackageManager));
183 }
184 }
185
186 }