| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.memconsumer; | 5 package org.chromium.memconsumer; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.ComponentName; |
| 9 import android.content.Context; |
| 8 import android.content.Intent; | 10 import android.content.Intent; |
| 9 import android.content.ServiceConnection; | 11 import android.content.ServiceConnection; |
| 10 import android.content.ComponentName; | |
| 11 import android.content.Context; | |
| 12 import android.os.Bundle; | 12 import android.os.Bundle; |
| 13 import android.os.IBinder; | 13 import android.os.IBinder; |
| 14 import android.view.Gravity; |
| 14 import android.view.KeyEvent; | 15 import android.view.KeyEvent; |
| 15 import android.view.Gravity; | |
| 16 import android.view.View; | 16 import android.view.View; |
| 17 import android.widget.EditText; | 17 import android.widget.EditText; |
| 18 import android.widget.NumberPicker; | 18 import android.widget.NumberPicker; |
| 19 import android.widget.TextView; | 19 import android.widget.TextView; |
| 20 | 20 |
| 21 public class MemConsumer extends Activity { | 21 public class MemConsumer extends Activity { |
| 22 public static final String NOTIFICATION_ACTION = | 22 public static final String NOTIFICATION_ACTION = |
| 23 MemConsumer.class.toString() + ".NOTIFICATION"; | 23 MemConsumer.class.toString() + ".NOTIFICATION"; |
| 24 | 24 |
| 25 private ResidentService mResidentService; | 25 private ResidentService mResidentService; |
| 26 private int mMemory = 0; | 26 private int mMemory = 0; |
| 27 private NumberPicker mMemoryPicker; | 27 private NumberPicker mMemoryPicker; |
| 28 | 28 |
| 29 private ServiceConnection mServiceConnection = new ServiceConnection() { | 29 private ServiceConnection mServiceConnection = new ServiceConnection() { |
| 30 | 30 @Override |
| 31 public void onServiceConnected(ComponentName name, | 31 public void onServiceConnected(ComponentName name, IBinder binder) { |
| 32 IBinder binder) { | 32 mResidentService = ((ResidentService.ServiceBinder) binder).getServi
ce(); |
| 33 mResidentService = ((ResidentService.ServiceBinder)binder).getServic
e(); | |
| 34 mResidentService.useMemory(mMemory); | 33 mResidentService.useMemory(mMemory); |
| 35 } | 34 } |
| 36 | 35 |
| 36 @Override |
| 37 public void onServiceDisconnected(ComponentName name) { | 37 public void onServiceDisconnected(ComponentName name) { |
| 38 mResidentService = null; | 38 mResidentService = null; |
| 39 } | 39 } |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 @Override | 42 @Override |
| 43 protected void onCreate(Bundle savedInstanceState) { | 43 protected void onCreate(Bundle savedInstanceState) { |
| 44 super.onCreate(savedInstanceState); | 44 super.onCreate(savedInstanceState); |
| 45 mMemoryPicker = new NumberPicker(this); | 45 mMemoryPicker = new NumberPicker(this); |
| 46 mMemoryPicker.setGravity(Gravity.CENTER); | 46 mMemoryPicker.setGravity(Gravity.CENTER); |
| 47 mMemoryPicker.setMaxValue(Integer.MAX_VALUE); | 47 mMemoryPicker.setMaxValue(Integer.MAX_VALUE); |
| 48 mMemoryPicker.setMinValue(0); | 48 mMemoryPicker.setMinValue(0); |
| 49 mMemoryPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeLi
stener() { | 49 mMemoryPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeLi
stener() { |
| 50 @Override | 50 @Override |
| 51 public void onValueChange(NumberPicker picker, int oldVal, int newVa
l) { | 51 public void onValueChange(NumberPicker picker, int oldVal, int newVa
l) { |
| 52 updateMemoryConsumption(picker.getValue()); | 52 updateMemoryConsumption(picker.getValue()); |
| 53 } | 53 } |
| 54 }); | 54 }); |
| 55 for (int i = 0; i < mMemoryPicker.getChildCount(); i++) { | 55 for (int i = 0; i < mMemoryPicker.getChildCount(); i++) { |
| 56 View child = mMemoryPicker.getChildAt(i); | 56 View child = mMemoryPicker.getChildAt(i); |
| 57 if (child instanceof EditText) { | 57 if (child instanceof EditText) { |
| 58 EditText editText = (EditText)child; | 58 EditText editText = (EditText) child; |
| 59 editText.setOnEditorActionListener(new TextView.OnEditorActionLi
stener() { | 59 editText.setOnEditorActionListener(new TextView.OnEditorActionLi
stener() { |
| 60 @Override | 60 @Override |
| 61 public boolean onEditorAction (TextView v, int actionId, Key
Event event) { | 61 public boolean onEditorAction (TextView v, int actionId, Key
Event event) { |
| 62 if (v.getText().length() > 0) { | 62 if (v.getText().length() > 0) { |
| 63 updateMemoryConsumption(Integer.parseInt(v.getText()
.toString())); | 63 updateMemoryConsumption(Integer.parseInt(v.getText()
.toString())); |
| 64 } | 64 } |
| 65 return false; | 65 return false; |
| 66 } | 66 } |
| 67 }); | 67 }); |
| 68 } | 68 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 98 } else { | 98 } else { |
| 99 mResidentService.useMemory(mMemory); | 99 mResidentService.useMemory(mMemory); |
| 100 if (mMemory == 0) { | 100 if (mMemory == 0) { |
| 101 unbindService(mServiceConnection); | 101 unbindService(mServiceConnection); |
| 102 stopService(new Intent(this, ResidentService.class)); | 102 stopService(new Intent(this, ResidentService.class)); |
| 103 mResidentService = null; | 103 mResidentService = null; |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 } | 107 } |
| OLD | NEW |