OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 package org.chromium.memconsumer; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.ComponentName; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.content.ServiceConnection; |
| 12 import android.os.Bundle; |
| 13 import android.os.IBinder; |
| 14 import android.view.Gravity; |
| 15 import android.view.KeyEvent; |
| 16 import android.view.View; |
| 17 import android.widget.EditText; |
| 18 import android.widget.NumberPicker; |
| 19 import android.widget.TextView; |
| 20 |
| 21 public class MemConsumer extends Activity { |
| 22 public static final String NOTIFICATION_ACTION = |
| 23 MemConsumer.class.toString() + ".NOTIFICATION"; |
| 24 |
| 25 private ResidentService mResidentService; |
| 26 private int mMemory = 0; |
| 27 private NumberPicker mMemoryPicker; |
| 28 |
| 29 private ServiceConnection mServiceConnection = new ServiceConnection() { |
| 30 @Override |
| 31 public void onServiceConnected(ComponentName name, IBinder binder) { |
| 32 mResidentService = ((ResidentService.ServiceBinder) binder).getServi
ce(); |
| 33 mResidentService.useMemory(mMemory); |
| 34 } |
| 35 |
| 36 @Override |
| 37 public void onServiceDisconnected(ComponentName name) { |
| 38 mResidentService = null; |
| 39 } |
| 40 }; |
| 41 |
| 42 @Override |
| 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.OnValueChangeLi
stener() { |
| 50 @Override |
| 51 public void onValueChange(NumberPicker picker, int oldVal, int newVa
l) { |
| 52 updateMemoryConsumption(picker.getValue()); |
| 53 } |
| 54 }); |
| 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.OnEditorActionLi
stener() { |
| 60 @Override |
| 61 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { |
| 62 if (v.getText().length() > 0) { |
| 63 updateMemoryConsumption(Integer.parseInt(v.getText()
.toString())); |
| 64 } |
| 65 return false; |
| 66 } |
| 67 }); |
| 68 } |
| 69 } |
| 70 setContentView(mMemoryPicker); |
| 71 onNewIntent(getIntent()); |
| 72 } |
| 73 |
| 74 @Override |
| 75 protected void onNewIntent(Intent intent) { |
| 76 super.onNewIntent(intent); |
| 77 if (intent.getAction() == NOTIFICATION_ACTION) { |
| 78 updateMemoryConsumption(0); |
| 79 return; |
| 80 } |
| 81 if (!intent.hasExtra("memory")) return; |
| 82 updateMemoryConsumption(intent.getIntExtra("memory", 0)); |
| 83 } |
| 84 |
| 85 void updateMemoryConsumption(int memory) { |
| 86 if (memory == mMemory || memory < 0) return; |
| 87 mMemory = memory; |
| 88 mMemoryPicker.setValue(mMemory); |
| 89 if (mResidentService == null) { |
| 90 if (mMemory > 0) { |
| 91 Intent resident = new Intent(); |
| 92 resident.setClass(this, ResidentService.class); |
| 93 startService(resident); |
| 94 bindService(new Intent(this, ResidentService.class), |
| 95 mServiceConnection, |
| 96 Context.BIND_AUTO_CREATE); |
| 97 } |
| 98 } else { |
| 99 mResidentService.useMemory(mMemory); |
| 100 if (mMemory == 0) { |
| 101 unbindService(mServiceConnection); |
| 102 stopService(new Intent(this, ResidentService.class)); |
| 103 mResidentService = null; |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
OLD | NEW |