Add license declaration and credit
[0xdroid:packages_apps_videotransmissionmonitor.git] / src / org / zeroxlab / vtm / MonitorActivity.java
1 /*
2 * Authored By Kanru Chen <kanru@0xlab.org>
3 *
4 * Copyright (c) 2011 0xlab.org - http://0xlab.org/
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.zeroxlab.vtm;
20
21 import android.app.Activity;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.res.Resources;
27 import android.graphics.Bitmap;
28 import android.graphics.BitmapFactory.Options;
29 import android.graphics.BitmapFactory;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.net.Uri;
32 import android.os.Bundle;
33 import android.util.Log;
34 import android.view.View;
35 import android.view.Window;
36 import android.view.animation.AnimationUtils;
37 import android.widget.FrameLayout.LayoutParams;
38 import android.widget.ImageSwitcher;
39 import android.widget.ImageView;
40 import android.widget.TextView;
41 import android.widget.ViewSwitcher;
42
43 import java.util.ArrayList;
44 import java.util.concurrent.LinkedBlockingQueue;
45
46 public class MonitorActivity extends Activity
47     implements ViewSwitcher.ViewFactory {
48
49     private static final boolean mDbg = true;
50
51     public static final String PROGRESS = "progress";
52     public static final String THUMBNAIL = "thumbnail";
53
54     private ImageSwitcher mSwitcher;
55
56     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
57             @Override
58             public void onReceive(Context context, Intent intent) {
59                 MonitorActivity.this.receivedBroadcast(intent);
60             }
61         };
62
63     @Override
64     protected void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         requestWindowFeature(Window.FEATURE_NO_TITLE);
67         setContentView(R.layout.main);
68         mSwitcher = (ImageSwitcher) findViewById(R.id.image);
69         mSwitcher.setFactory(this);
70         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
71                 android.R.anim.fade_in));
72         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
73                 android.R.anim.fade_out));
74     }
75
76     public View makeView() {
77         ImageView i = new ImageView(this);
78         i.setBackgroundColor(0xFF000000);
79         i.setScaleType(ImageView.ScaleType.CENTER_CROP);
80         i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
81                                                          LayoutParams.MATCH_PARENT));
82         return i;
83     }
84
85     @Override
86     protected void onNewIntent(Intent intent) {
87         setIntent(intent);
88     }
89
90     ArrayList<String> thumbnails;
91     Uri data;
92     String type;
93
94     @Override
95     protected void onResume() {
96         super.onResume();
97         Intent intent = getIntent();
98         IntentFilter iff = new IntentFilter();
99         iff.addAction("org.zeroxlab.vtm.intent.UPDATE");
100         this.registerReceiver(this.mBroadcastReceiver, iff);
101         thumbnails = intent.getStringArrayListExtra(THUMBNAIL);
102         data = intent.getData();
103         type = intent.getType();
104         receivedBroadcast(intent);
105         Thread update = new Thread(new Runnable() {
106                 @Override
107                 public void run() {
108                     int step;
109                     if (thumbnails != null)
110                         step = 15 / thumbnails.size();
111                     else
112                         step = 5;
113                     for (int i = 0; i <= 100; i += step) {
114                         Intent intent = new Intent("org.zeroxlab.vtm.intent.UPDATE");
115                         intent.putExtra("progress", i);
116                         MonitorActivity.this.sendBroadcast(intent);
117                         try {
118                             Thread.sleep(500);
119                         } catch (InterruptedException e) {
120                             return;
121                         }
122                     }
123                     Intent intent = new Intent("org.zeroxlab.vtm.intent.UPDATE");
124                     intent.putExtra("progress", 100);
125                     MonitorActivity.this.sendBroadcast(intent);
126                 }
127             });
128         update.start();
129     }
130
131     @Override
132     public void onPause() {
133         super.onPause();
134         this.unregisterReceiver(this.mBroadcastReceiver);
135     }
136
137     LinkedBlockingQueue<Bitmap> imgQueue = new LinkedBlockingQueue<Bitmap>(2);
138     String thumb_path = null;
139
140     private void receivedBroadcast(Intent i) {
141         Intent intent = i;
142
143         int percent = intent.getIntExtra(PROGRESS, 0);
144         TextView text = (TextView)findViewById(R.id.text);
145
146         text.setText(String.valueOf(percent) + "%");
147
148         String new_path = null;
149         if (thumbnails != null) {
150             int n = (int)Math.ceil(thumbnails.size() * ((float)percent/100))-1;
151             if (n < 0)
152                 n = 0;
153             if (n > thumbnails.size()-1)
154                 n = thumbnails.size()-1;
155             new_path = thumbnails.get(n);
156         }
157         if (new_path != null && new_path != thumb_path) {
158             thumb_path = new_path;
159             Resources res = getResources();
160             BitmapFactory.Options opts = new BitmapFactory.Options();
161             opts.inPurgeable = true;
162             opts.inInputShareable = true;
163             if (imgQueue.size() >= 2) {
164                 imgQueue.poll().recycle();
165             }
166             Bitmap bmp = BitmapFactory.decodeFile(thumb_path, opts);
167             BitmapDrawable pic = new BitmapDrawable(bmp);
168             mSwitcher.setImageDrawable(pic);
169             imgQueue.offer(bmp);
170         }
171         if (percent == 100) {
172             intent = new Intent(Intent.ACTION_VIEW);
173             intent.setDataAndType(data, type);
174             startActivity(intent);
175             finish();
176         }
177     }
178
179     @Override
180     public void onDestroy() {
181         super.onDestroy();
182         while (imgQueue.size() > 0) {
183             imgQueue.poll().recycle();
184         }
185     }
186 }