1 /*
2  * Copyright 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.notificationstudio.action;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.net.Uri;
24 import android.util.Log;
25 import android.view.View;
26 import android.widget.Toast;
27 
28 import com.android.notificationstudio.R;
29 
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 
37 public class ShareMockupAction {
38     private static final String TAG = ShareMockupAction.class.getSimpleName();
39 
40     private static final SimpleDateFormat FILE_NAME =
41             new SimpleDateFormat("'notification.'yyyyMMdd'.'HHmmss'.png'");
42 
launch(Activity activity, CharSequence title)43     public static void launch(Activity activity, CharSequence title) {
44         // take a picture of the current mockup
45         View v = activity.findViewById(R.id.preview);
46         int w = v.getMeasuredWidth();
47         int h = v.getMeasuredHeight();
48         Bitmap mockup = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
49         Canvas c = new Canvas(mockup);
50         v.layout(0, 0, w, h);
51         v.draw(c);
52 
53         // write the mockup to a temp file
54         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
55         mockup.compress(Bitmap.CompressFormat.PNG, 100, bytes);
56         File f = new File(activity.getExternalCacheDir(), FILE_NAME.format(new Date()));
57         FileOutputStream fo = null;
58         try {
59             f.createNewFile();
60             fo = new FileOutputStream(f);
61             fo.write(bytes.toByteArray());
62         } catch (IOException e) {
63             String msg = "Error writing mockup file";
64             Log.w(TAG, msg, e);
65             Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
66             return;
67         } finally {
68             if (fo != null)
69                try { fo.close(); } catch (Exception e) { }
70         }
71 
72         // launch intent to send the mockup image
73         Intent share = new Intent(Intent.ACTION_SEND);
74         share.setType("image/png");
75         share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f.getAbsoluteFile()));
76         activity.startActivity(Intent.createChooser(share, title));
77     }
78 
79 }
80