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