1/*
2 * Copyright (C) 2011 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
17package com.google.android.test.activity;
18
19import java.util.ArrayList;
20import java.util.List;
21
22import android.app.Activity;
23import android.app.ActivityManager;
24import android.app.ActivityThread;
25import android.app.AlertDialog;
26import android.app.Application;
27import android.content.ActivityNotFoundException;
28import android.os.Bundle;
29import android.graphics.BitmapFactory;
30import android.graphics.Bitmap;
31import android.graphics.Canvas;
32import android.graphics.drawable.BitmapDrawable;
33import android.graphics.drawable.Drawable;
34import android.widget.ImageView;
35import android.widget.LinearLayout;
36import android.widget.TextView;
37import android.widget.ScrollView;
38import android.view.LayoutInflater;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.view.View;
42import android.content.Context;
43import android.content.pm.ApplicationInfo;
44import android.content.pm.PackageManager;
45import android.content.res.CompatibilityInfo;
46import android.util.DisplayMetrics;
47import android.util.Log;
48
49public class ActivityTestMain extends Activity {
50    ActivityManager mAm;
51
52    private void addThumbnail(LinearLayout container, Bitmap bm,
53            final ActivityManager.RecentTaskInfo task,
54            final ActivityManager.TaskThumbnails thumbs, final int subIndex) {
55        ImageView iv = new ImageView(this);
56        if (bm != null) {
57            iv.setImageBitmap(bm);
58        }
59        iv.setBackgroundResource(android.R.drawable.gallery_thumb);
60        int w = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_width);
61        int h = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_height);
62        container.addView(iv, new LinearLayout.LayoutParams(w, h));
63
64        iv.setOnClickListener(new View.OnClickListener() {
65            @Override
66            public void onClick(View v) {
67                if (task.id >= 0 && thumbs != null) {
68                    if (subIndex < (thumbs.numSubThumbbails-1)) {
69                        mAm.removeSubTask(task.id, subIndex+1);
70                    }
71                    mAm.moveTaskToFront(task.id, ActivityManager.MOVE_TASK_WITH_HOME);
72                } else {
73                    try {
74                        startActivity(task.baseIntent);
75                    } catch (ActivityNotFoundException e) {
76                        Log.w("foo", "Unable to start task: " + e);
77                    }
78                }
79                buildUi();
80            }
81        });
82        iv.setOnLongClickListener(new View.OnLongClickListener() {
83            @Override
84            public boolean onLongClick(View v) {
85                if (task.id >= 0 && thumbs != null) {
86                    if (subIndex < 0) {
87                        mAm.removeTask(task.id, ActivityManager.REMOVE_TASK_KILL_PROCESS);
88                    } else {
89                        mAm.removeSubTask(task.id, subIndex);
90                    }
91                    buildUi();
92                    return true;
93                }
94                return false;
95            }
96        });
97    }
98
99    @Override
100    protected void onCreate(Bundle savedInstanceState) {
101        super.onCreate(savedInstanceState);
102
103        mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
104    }
105
106    @Override
107    public boolean onCreateOptionsMenu(Menu menu) {
108        menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
109            @Override public boolean onMenuItemClick(MenuItem item) {
110                AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this,
111                        R.style.SlowDialog);
112                builder.setTitle("This is a title");
113                builder.show();
114                return true;
115            }
116        });
117        return true;
118    }
119
120    @Override
121    protected void onStart() {
122        super.onStart();
123        buildUi();
124    }
125
126    private View scrollWrap(View view) {
127        ScrollView scroller = new ScrollView(this);
128        scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
129                ScrollView.LayoutParams.MATCH_PARENT));
130        return scroller;
131    }
132
133    private void buildUi() {
134        LinearLayout top = new LinearLayout(this);
135        top.setOrientation(LinearLayout.VERTICAL);
136
137        List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10,
138                ActivityManager.RECENT_WITH_EXCLUDED);
139        if (recents != null) {
140            for (int i=0; i<recents.size(); i++) {
141                ActivityManager.RecentTaskInfo r = recents.get(i);
142                ActivityManager.TaskThumbnails tt = mAm.getTaskThumbnails(r.persistentId);
143                TextView tv = new TextView(this);
144                tv.setText(r.baseIntent.getComponent().flattenToShortString());
145                top.addView(tv, new LinearLayout.LayoutParams(
146                        LinearLayout.LayoutParams.WRAP_CONTENT,
147                        LinearLayout.LayoutParams.WRAP_CONTENT));
148                LinearLayout item = new LinearLayout(this);
149                item.setOrientation(LinearLayout.HORIZONTAL);
150                addThumbnail(item, tt != null ? tt.mainThumbnail : null, r, tt, -1);
151                for (int j=0; j<tt.numSubThumbbails; j++) {
152                    addThumbnail(item, tt.getSubThumbnail(j), r, tt, j);
153                }
154                top.addView(item, new LinearLayout.LayoutParams(
155                        LinearLayout.LayoutParams.WRAP_CONTENT,
156                        LinearLayout.LayoutParams.WRAP_CONTENT));
157            }
158        }
159
160        setContentView(scrollWrap(top));
161    }
162}
163