summaryrefslogtreecommitdiffstats
path: root/packages/FakeOemFeatures/src/com/android/fakeoemfeatures/FakeApp.java
blob: 436e57976c98455da205b08346390afa86d6a30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.fakeoemfeatures;

import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Slog;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;

public class FakeApp extends Application {
    // Stuffing of 20MB
    static final int STUFFING_SIZE_BYTES = 20*1024*1024;
    static final int STUFFING_SIZE_INTS = STUFFING_SIZE_BYTES/4;
    int[] mStuffing;

    // Assume 4k pages.
    static final int PAGE_SIZE = 4*1024;

    static final long TICK_DELAY = 4*60*60*1000; // One hour
    static final int MSG_TICK = 1;
    final Handler mHandler = new Handler() {
        @Override public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TICK:
                    // Our service is IMPORTANT.  We know, we wrote it.
                    // We need to keep that thing running.  Because WE KNOW.
                    // Damn you users, STOP MESSING WITH US.
                    startService(new Intent(FakeApp.this, FakeBackgroundService.class));
                    sendEmptyMessageDelayed(MSG_TICK, TICK_DELAY);
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };

    // Always run another process for more per-process overhead.
    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
        }

        @Override public void onServiceDisconnected(ComponentName name) {
        }
    };
    ServiceConnection mServiceConnection2 = new ServiceConnection() {
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
        }

        @Override public void onServiceDisconnected(ComponentName name) {
        }
    };
    ServiceConnection mServiceConnection3 = new ServiceConnection() {
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
        }

        @Override public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    public void onCreate() {
        String processName = ActivityThread.currentPackageName();
        Slog.i("FakeOEMFeatures", "Creating app in process: " + processName);
        if (!getApplicationInfo().packageName.equals(processName)) {
            // If we are not in the main process of the app, then don't do
            // our extra overhead stuff.
            return;
        }

        final WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        final Display display = wm.getDefaultDisplay();

        // Check to make sure we are not running on a user build.  If this
        // is a user build, WARN!  Do not want!
        if ("user".equals(android.os.Build.TYPE)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Should not be on user build");
            builder.setMessage("The app Fake OEM Features should not be installed on a "
                    + "user build.  Please remove this .apk before shipping this build to "
                    + " your customers!");
            builder.setCancelable(false);
            builder.setPositiveButton("I understand", null);
            Dialog dialog = builder.create();
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.show();
        }

        // Make a fake window that is always around eating graphics resources.
        FakeView view = new FakeView(this);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        if (ActivityManager.isHighEndGfx(display)) {
            lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
        }
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        int maxSize = display.getMaximumSizeDimension();
        maxSize *= 2;
        lp.x = maxSize;
        lp.y = maxSize;
        lp.setTitle(getPackageName());
        wm.addView(view, lp);

        // Bind to a fake service we want to keep running in another process.
        bindService(new Intent(this, FakeCoreService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
        bindService(new Intent(this, FakeCoreService2.class), mServiceConnection2,
                Context.BIND_AUTO_CREATE);
        bindService(new Intent(this, FakeCoreService3.class), mServiceConnection3,
                Context.BIND_AUTO_CREATE);

        // Start to a fake service that should run in the background of
        // another process.
        mHandler.sendEmptyMessage(MSG_TICK);

        // Make a fake allocation to consume some RAM.
        mStuffing = new int[STUFFING_SIZE_INTS];
        for (int i=0; i<STUFFING_SIZE_BYTES/PAGE_SIZE; i++) {
            // Fill each page with a unique value.
            final int VAL = i*2 + 100;
            final int OFF = (i*PAGE_SIZE)/4;
            for (int j=0; j<(PAGE_SIZE/4); j++) {
                mStuffing[OFF+j] = VAL;
            }
        }
    }
}