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