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.Notification; 8 import android.app.PendingIntent; 9 import android.app.Service; 10 import android.content.Intent; 11 import android.os.Binder; 12 import android.os.IBinder; 13 14 public class ResidentService extends Service { 15 static { 16 // Loading the native library. 17 System.loadLibrary("memconsumer"); 18 } 19 20 public class ServiceBinder extends Binder { getService()21 ResidentService getService() { 22 return ResidentService.this; 23 } 24 } 25 26 private static final int RESIDENT_NOTIFICATION_ID = 1; 27 28 private final IBinder mBinder = new ServiceBinder(); 29 private boolean mIsInForeground = false; 30 31 @Override onBind(Intent intent)32 public IBinder onBind(Intent intent) { 33 return mBinder; 34 } 35 useMemory(long memory)36 public void useMemory(long memory) { 37 if (memory > 0) { 38 Intent notificationIntent = new Intent(this, MemConsumer.class); 39 notificationIntent.setAction(MemConsumer.NOTIFICATION_ACTION); 40 PendingIntent pendingIntent = 41 PendingIntent.getActivity(this, 0, notificationIntent, 0); 42 Notification notification = 43 new Notification.Builder(getApplicationContext()). 44 setContentTitle("MC running (" + memory + "Mb)"). 45 setSmallIcon(R.drawable.notification_icon). 46 setDeleteIntent(pendingIntent). 47 setContentIntent(pendingIntent). 48 build(); 49 startForeground(RESIDENT_NOTIFICATION_ID, notification); 50 mIsInForeground = true; 51 } 52 if (mIsInForeground && memory == 0) { 53 stopForeground(true); 54 mIsInForeground = false; 55 } 56 nativeUseMemory(memory * 1024 * 1024); 57 } 58 59 // Allocate the amount of memory in native code. Otherwise the memory 60 // allocation is limited by the framework. nativeUseMemory(long memory)61 private native void nativeUseMemory(long memory); 62 } 63