Merge pull request #912 from owncloud/review_license
[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.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.view.LayoutInflater;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.widget.ArrayAdapter;
42 import android.widget.ImageView;
43 import android.widget.TextView;
44
45 import com.actionbarsherlock.app.SherlockDialogFragment;
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 SherlockDialogFragment {
58
59 private final static String TAG = ShareLinkToDialog.class.getSimpleName();
60 private final static String ARG_INTENT = ShareLinkToDialog.class.getSimpleName() + ".ARG_INTENT";
61 private final static String ARG_PACKAGES_TO_EXCLUDE = ShareLinkToDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE";
62 private final static String ARG_FILE_TO_SHARE = ShareLinkToDialog.class.getSimpleName() + ".FILE_TO_SHARE";
63
64 private ActivityAdapter mAdapter;
65 private OCFile mFile;
66 private Intent mIntent;
67
68 public static ShareLinkToDialog newInstance(Intent intent, String[] packagesToExclude, OCFile fileToShare) {
69 ShareLinkToDialog f = new ShareLinkToDialog();
70 Bundle args = new Bundle();
71 args.putParcelable(ARG_INTENT, intent);
72 args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude);
73 args.putParcelable(ARG_FILE_TO_SHARE, fileToShare);
74 f.setArguments(args);
75 return f;
76 }
77
78 public ShareLinkToDialog() {
79 super();
80 Log_OC.d(TAG, "constructor");
81 }
82
83 @Override
84 public Dialog onCreateDialog(Bundle savedInstanceState) {
85 mIntent = getArguments().getParcelable(ARG_INTENT);
86 String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE);
87 List<String> packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]);
88 mFile = getArguments().getParcelable(ARG_FILE_TO_SHARE);
89
90 PackageManager pm= getSherlockActivity().getPackageManager();
91 List<ResolveInfo> activities = pm.queryIntentActivities(mIntent, PackageManager.MATCH_DEFAULT_ONLY);
92 Iterator<ResolveInfo> it = activities.iterator();
93 ResolveInfo resolveInfo;
94 while (it.hasNext()) {
95 resolveInfo = it.next();
96 if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) {
97 it.remove();
98 }
99 }
100
101 boolean sendAction = mIntent.getBooleanExtra(Intent.ACTION_SEND, false);
102
103 if (!sendAction) {
104 // add activity for copy to clipboard
105 Intent copyToClipboardIntent = new Intent(getSherlockActivity(), CopyToClipboardActivity.class);
106 List<ResolveInfo> copyToClipboard = pm.queryIntentActivities(copyToClipboardIntent, 0);
107 if (!copyToClipboard.isEmpty()) {
108 activities.add(copyToClipboard.get(0));
109 }
110 }
111
112 Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
113 mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities);
114
115 return createSelector(sendAction);
116
117 }
118
119 private AlertDialog createSelector(final boolean sendAction) {
120
121 int titleId;
122 if (sendAction) {
123 titleId = R.string.activity_chooser_send_file_title;
124 } else {
125 titleId = R.string.activity_chooser_title;
126 }
127
128 return new AlertDialog.Builder(getSherlockActivity())
129 .setTitle(titleId)
130 .setAdapter(mAdapter, new DialogInterface.OnClickListener() {
131 @Override
132 public void onClick(DialogInterface dialog, int which) {
133 // Add the information of the chosen activity to the intent to send
134 ResolveInfo chosen = mAdapter.getItem(which);
135 ActivityInfo actInfo = chosen.activityInfo;
136 ComponentName name=new ComponentName(
137 actInfo.applicationInfo.packageName,
138 actInfo.name);
139 mIntent.setComponent(name);
140
141 if (sendAction) {
142 dialog.dismiss(); // explicitly added for Android 2.x devices
143
144 // Send the file
145 ((FileActivity)getSherlockActivity()).startActivity(mIntent);
146
147 } else {
148 // Create a new share resource
149 ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper()
150 .shareFileWithLinkToApp(mFile, mIntent);
151 }
152 }
153 })
154 .create();
155 }
156
157 class ActivityAdapter extends ArrayAdapter<ResolveInfo> {
158
159 private PackageManager mPackageManager;
160
161 ActivityAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
162 super(context, R.layout.activity_row, apps);
163 this.mPackageManager = pm;
164 }
165
166 @Override
167 public View getView(int position, View convertView, ViewGroup parent) {
168 if (convertView == null) {
169 convertView = newView(parent);
170 }
171 bindView(position, convertView);
172 return convertView;
173 }
174
175 private View newView(ViewGroup parent) {
176 return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).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 }