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