• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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.fakeoemfeatures;
18 
19 import static android.view.Display.DEFAULT_DISPLAY;
20 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
21 
22 import android.app.Dialog;
23 import android.app.Service;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.graphics.Rect;
27 import android.hardware.display.DisplayManager;
28 import android.os.Handler;
29 import android.os.IBinder;
30 import android.os.Message;
31 import android.view.Display;
32 import android.view.ViewGroup;
33 import android.view.WindowManager;
34 
35 import java.util.ArrayList;
36 import java.util.Random;
37 
38 public class FakeBackgroundService extends Service {
39     final ArrayList<int[]> mAllocs = new ArrayList<int[]>();
40 
41     final Random mRandom = new Random();
42 
43     static final long TICK_DELAY = 30*1000; // 30 seconds
44     static final int MSG_TICK = 1;
45     final Handler mHandler = new Handler() {
46         @Override public void handleMessage(Message msg) {
47             switch (msg.what) {
48                 case MSG_TICK:
49                     // We are awesome!  To prove we are doing awesome stuff,
50                     // we must use some memory!  It wouldn't be awesome if
51                     // we didn't use memory!
52                     for (int i=0; i<5; i++) {
53                         try {
54                             int[] alloc = new int[FakeApp.PAGE_SIZE/4];
55                             mAllocs.add(alloc);
56                             final int VAL = mRandom.nextInt();
57                             for (int j=0; j<FakeApp.PAGE_SIZE/4; j++) {
58                                 alloc[j] = VAL;
59                             }
60                         } catch (OutOfMemoryError e) {
61                         }
62                     }
63                     sendEmptyMessageDelayed(MSG_TICK, TICK_DELAY);
64                     break;
65                 default:
66                     super.handleMessage(msg);
67                     break;
68             }
69         }
70     };
71 
onCreate()72     @Override public void onCreate() {
73         super.onCreate();
74         mHandler.sendEmptyMessageDelayed(MSG_TICK, TICK_DELAY);
75 
76         final DisplayManager dm = getSystemService(DisplayManager.class);
77         final Display display = dm.getDisplay(DEFAULT_DISPLAY);
78         final Context windowContext = createDisplayContext(display)
79                 .createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */);
80 
81         // Make a fake window that is always around eating graphics resources.
82         FakeView view = new FakeView(windowContext);
83         Dialog dialog = new Dialog(windowContext, android.R.style.Theme_Holo_Dialog);
84         dialog.getWindow().setType(TYPE_APPLICATION_OVERLAY);
85         dialog.getWindow().setFlags(
86                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
87                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
88                 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
89                 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
90                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
91                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
92                 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
93                 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
94                 | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
95         dialog.getWindow().setDimAmount(0);
96         dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
97                 ViewGroup.LayoutParams.MATCH_PARENT);
98         WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
99         // Create an instance of WindowManager that is adjusted to the area of the display dedicated
100         // for windows with type TYPE_APPLICATION_OVERLAY.
101         final WindowManager wm = windowContext.getSystemService(WindowManager.class);
102         Rect maxWindowBounds = wm.getMaximumWindowMetrics().getBounds();
103         int maxSize = Math.max(maxWindowBounds.width(), maxWindowBounds.height());
104         maxSize *= 2;
105         lp.x = maxSize;
106         lp.y = maxSize;
107         lp.setTitle(getPackageName() + ":background");
108         dialog.getWindow().setAttributes(lp);
109         dialog.getWindow().setContentView(view);
110         dialog.show();
111     }
112 
onDestroy()113     @Override public void onDestroy() {
114         super.onDestroy();
115         mHandler.removeMessages(MSG_TICK);
116     }
117 
onBind(Intent intent)118     @Override public IBinder onBind(Intent intent) {
119         return null;
120     }
121 }
122