1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.memconsumer;
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.content.ServiceConnection;
10 import android.content.ComponentName;
11 import android.content.Context;
12 import android.os.Bundle;
13 import android.os.IBinder;
14 import android.view.KeyEvent;
15 import android.view.Gravity;
16 import android.view.View;
17 import android.widget.EditText;
18 import android.widget.NumberPicker;
19 import android.widget.TextView;
21 public class MemConsumer extends Activity {
22 public static final String NOTIFICATION_ACTION =
23 MemConsumer.class.toString() + ".NOTIFICATION";
25 private ResidentService mResidentService;
26 private int mMemory = 0;
27 private NumberPicker mMemoryPicker;
29 private ServiceConnection mServiceConnection = new ServiceConnection() {
31 public void onServiceConnected(ComponentName name,
33 mResidentService = ((ResidentService.ServiceBinder)binder).getService();
34 mResidentService.useMemory(mMemory);
37 public void onServiceDisconnected(ComponentName name) {
38 mResidentService = null;
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 mMemoryPicker = new NumberPicker(this);
46 mMemoryPicker.setGravity(Gravity.CENTER);
47 mMemoryPicker.setMaxValue(Integer.MAX_VALUE);
48 mMemoryPicker.setMinValue(0);
49 mMemoryPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
51 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
52 updateMemoryConsumption(picker.getValue());
55 for (int i = 0; i < mMemoryPicker.getChildCount(); i++) {
56 View child = mMemoryPicker.getChildAt(i);
57 if (child instanceof EditText) {
58 EditText editText = (EditText)child;
59 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
61 public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
62 if (v.getText().length() > 0) {
63 updateMemoryConsumption(Integer.parseInt(v.getText().toString()));
70 setContentView(mMemoryPicker);
71 onNewIntent(getIntent());
75 protected void onNewIntent(Intent intent) {
76 super.onNewIntent(intent);
77 if (intent.getAction() == NOTIFICATION_ACTION) {
78 updateMemoryConsumption(0);
81 if (!intent.hasExtra("memory")) return;
82 updateMemoryConsumption(intent.getIntExtra("memory", 0));
85 void updateMemoryConsumption(int memory) {
86 if (memory == mMemory || memory < 0) return;
88 mMemoryPicker.setValue(mMemory);
89 if (mResidentService == null) {
91 Intent resident = new Intent();
92 resident.setClass(this, ResidentService.class);
93 startService(resident);
94 bindService(new Intent(this, ResidentService.class),
96 Context.BIND_AUTO_CREATE);
99 mResidentService.useMemory(mMemory);
101 unbindService(mServiceConnection);
102 stopService(new Intent(this, ResidentService.class));
103 mResidentService = null;