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.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;
14 public class ResidentService extends Service {
16 // Loading the native library.
17 System.loadLibrary("memconsumer");
20 public class ServiceBinder extends Binder {
21 ResidentService getService() {
22 return ResidentService.this;
26 private static final int RESIDENT_NOTIFICATION_ID = 1;
28 private final IBinder mBinder = new ServiceBinder();
29 private boolean mIsInForeground = false;
32 public IBinder onBind(Intent intent) {
36 public void useMemory(long memory) {
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)
49 startForeground(RESIDENT_NOTIFICATION_ID, notification);
50 mIsInForeground = true;
52 if (mIsInForeground && memory == 0) {
54 mIsInForeground = false;
56 nativeUseMemory(memory * 1024 * 1024);
59 // Allocate the amount of memory in native code. Otherwise the memory
60 // allocation is limited by the framework.
61 private native void nativeUseMemory(long memory);