1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.camera.ui;
import com.android.camera.R;
import com.android.camera.Util;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// A popup window that contains a big thumbnail and a list of apps to share.
public class SharePopup extends PopupWindow implements View.OnClickListener,
View.OnTouchListener, AdapterView.OnItemClickListener {
private static final String TAG = "SharePopup";
private static final String ADAPTER_COLUMN_ICON = "icon";
private Context mContext;
private Uri mUri;
private String mMimeType;
private ImageView mThumbnail;
private int mBitmapWidth;
private int mBitmapHeight;
private int mOrientation;
// A view that contains a thumbnail and an arrow icon.
private ViewGroup mShareView;
// A view that contains a list of application icons and the share view.
private View mRootView;
// The list of the application icons.
private ListView mShareList;
// A rotated view that contains the thumbnail.
private RotateLayout mThumbnailRotateLayout;
private View mPreviewFrame;
private ArrayList<ComponentName> mComponent = new ArrayList<ComponentName>();
private class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
RotateLayout r = (RotateLayout) v.findViewById(R.id.share_icon_rotate_layout);
r.setOrientation(mOrientation);
return v;
}
}
private final SimpleAdapter.ViewBinder mViewBinder =
new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(final View view, final Object data,
final String text) {
if (view instanceof ImageView) {
((ImageView)view).setImageDrawable((Drawable) data);
return true;
}
return false;
}
};
public SharePopup(Activity activity, Uri uri, Bitmap bitmap, int orientation,
View previewFrame) {
super(activity);
// Initialize variables
mContext = activity;
mUri = uri;
mMimeType = mContext.getContentResolver().getType(mUri);
mPreviewFrame = previewFrame;
LayoutInflater inflater = activity.getLayoutInflater();
ViewGroup sharePopup = (ViewGroup) inflater.inflate(R.layout.share_popup, null, false);
// This is required because popup window is full screen.
sharePopup.setOnTouchListener(this);
mThumbnailRotateLayout = (RotateLayout) sharePopup.findViewById(R.id.thumbnail_rotate_layout);
mShareList = (ListView) sharePopup.findViewById(R.id.share_list);
mShareList.setDivider(null);
mThumbnail = (ImageView) sharePopup.findViewById(R.id.thumbnail);
mThumbnail.setImageBitmap(bitmap);
mShareView = (ViewGroup) sharePopup.findViewById(R.id.share_view);
mShareView.setOnClickListener(this);
mBitmapWidth = bitmap.getWidth();
mBitmapHeight = bitmap.getHeight();
// Show play button if this is a video thumbnail.
if (mMimeType.startsWith("video/")) {
sharePopup.findViewById(R.id.play).setVisibility(View.VISIBLE);
// for some reason we want to show video thumbnail in a 4/3 ratio
// crop the image here, for dispaly, as necessary
final float targetRatio = 4F/3F;
final float existingRatio = (float)mBitmapWidth / (float)mBitmapHeight;
if (existingRatio > targetRatio) {
int newWidth = (int) ((float)mBitmapHeight * targetRatio);
// check if we end up with the same width due to rounding
if (newWidth != mBitmapWidth) {
bitmap = Bitmap.createBitmap(
bitmap,
(mBitmapWidth - newWidth) / 2,
0, // yCoord
newWidth,
mBitmapHeight,
null,
false);
}
} else if (existingRatio < targetRatio) {
int newHeight = (int) ((float)mBitmapWidth * targetRatio);
// check if we end up with the same width due to rounding
if (newHeight != mBitmapHeight) {
bitmap = Bitmap.createBitmap(
bitmap,
0, // xCoord
(mBitmapHeight - newHeight) / 2,
mBitmapWidth,
newHeight,
null,
false);
}
}
}
mBitmapWidth = bitmap.getWidth();
mBitmapHeight = bitmap.getHeight();
Resources res = mContext.getResources();
// Initialize popup window size.
mRootView = sharePopup.findViewById(R.id.root);
LayoutParams params = mRootView.getLayoutParams();
params.width = previewFrame.getWidth();
params.height = previewFrame.getHeight();
mRootView.setLayoutParams(params);
// Initialize popup window.
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(new ColorDrawable());
setContentView(sharePopup);
setOrientation(orientation);
setFocusable(true);
setAnimationStyle(R.style.AnimationPopup);
createShareMenu();
adjustThumbnailPosition();
}
private void adjustThumbnailPosition() {
RelativeLayout.LayoutParams lpOld =
(RelativeLayout.LayoutParams) mThumbnailRotateLayout.getLayoutParams();
RelativeLayout.LayoutParams lpNew =
new RelativeLayout.LayoutParams(lpOld.width, lpOld.height);
mRootView.setBackgroundDrawable(null);
if (mBitmapWidth > mBitmapHeight*2) {
// panorama image
lpNew.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lpNew.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpNew.addRule(RelativeLayout.CENTER_VERTICAL);
// panorama images block the preview from showing in the background
// use a special color here for that.
mRootView.setBackgroundColor(
mContext.getResources().getColor(R.color.share_popup_blackout));
} else if (mBitmapWidth > mBitmapHeight) {
// landscape image
lpNew.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpNew.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lpNew.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
} else {
// portrait image
lpNew.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpNew.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lpNew.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
mThumbnailRotateLayout.setLayoutParams(lpNew);
}
public void setOrientation(int orientation) {
mOrientation = orientation;
int hPaddingRootView = mRootView.getPaddingLeft() + mRootView.getPaddingRight();
int vPaddingRootView = mRootView.getPaddingTop() + mRootView.getPaddingBottom();
int hPadding = mShareView.getPaddingLeft() + mShareView.getPaddingRight();
int vPadding = mShareView.getPaddingTop() + mShareView.getPaddingBottom();
// Calculate the width and the height of the thumbnail. Reserve the
// space for paddings.
float maxWidth = mPreviewFrame.getWidth() - hPadding - hPaddingRootView;
float maxHeight = mPreviewFrame.getHeight() - vPadding - vPaddingRootView;
// Swap the width and height if it is portrait mode.
if (orientation == 90 || orientation == 270) {
float temp = maxWidth;
maxWidth = maxHeight;
maxHeight = temp;
}
float actualAspect = maxWidth / maxHeight;
float desiredAspect = (float) mBitmapWidth / mBitmapHeight;
LayoutParams params = mThumbnail.getLayoutParams();
if (actualAspect > desiredAspect) {
params.width = Math.round(maxHeight * desiredAspect);
params.height = Math.round(maxHeight);
} else {
params.width = Math.round(maxWidth);
params.height = Math.round(maxWidth / desiredAspect);
}
mThumbnail.setLayoutParams(params);
if (mThumbnailRotateLayout != null) mThumbnailRotateLayout.setOrientation(orientation);
int count = mShareList.getChildCount();
for (int i = 0; i < count; i++) {
ViewGroup f = (ViewGroup) mShareList.getChildAt(i);
RotateLayout r = (RotateLayout) f.findViewById(R.id.share_icon_rotate_layout);
r.setOrientation(orientation);
}
adjustThumbnailPosition();
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
super.showAtLocation(parent, gravity, x, y);
// Inform other popup to dismiss if exit
PopupManager.getInstance(mContext).notifyShowPopup(null);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share_view:
Util.viewUri(mUri, mContext);
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
dismiss();
return true;
}
return false;
}
public void createShareMenu() {
PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> infos = packageManager.queryIntentActivities(
new Intent(Intent.ACTION_SEND).setType(mMimeType), 0);
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
for (ResolveInfo info: infos) {
ComponentName component = new ComponentName(
info.activityInfo.packageName, info.activityInfo.name);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(ADAPTER_COLUMN_ICON, info.loadIcon(packageManager));
items.add(map);
mComponent.add(component);
}
SimpleAdapter listItemAdapter = new MySimpleAdapter(mContext, items,
R.layout.share_icon,
new String[] {ADAPTER_COLUMN_ICON},
new int[] {R.id.icon});
listItemAdapter.setViewBinder(mViewBinder);
mShareList.setAdapter(listItemAdapter);
mShareList.setOnItemClickListener(this);
}
public Uri getUri() {
return mUri;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(mMimeType);
intent.putExtra(Intent.EXTRA_STREAM, mUri);
intent.setComponent(mComponent.get(index));
mContext.startActivity(intent);
}
}
|