• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.stubs;;
18 
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.AsyncTask;
25 import android.os.Binder;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.os.Parcel;
29 import android.os.RemoteException;
30 
31 import java.util.concurrent.CountDownLatch;
32 
33 public class TrimMemService extends Service {
34     private static final int COMMAND_TRIM_MEMORY_LEVEL = IBinder.FIRST_CALL_TRANSACTION;
35     private Binder mRemote = new Binder();
36     private IBinder mCallback;
37 
38     @Override
onBind(Intent intent)39     public IBinder onBind(Intent intent) {
40         final Bundle extras = intent.getExtras();
41         mCallback = extras.getBinder(CommandReceiver.EXTRA_CALLBACK);
42         return mRemote;
43     }
44 
45     @Override
onTrimMemory(int level)46     public void onTrimMemory(int level) {
47         if (mCallback != null) {
48             Parcel data = Parcel.obtain();
49             Parcel reply = Parcel.obtain();
50             data.writeInt(level);
51             try {
52                 mCallback.transact(COMMAND_TRIM_MEMORY_LEVEL, data, reply, 0);
53             } catch (RemoteException e) {
54             } finally {
55                 data.recycle();
56                 reply.recycle();
57             }
58         }
59     }
60 
61     private static class MyMemFactorCallback extends Binder {
62         private CountDownLatch[] mLatchHolder;
63         private int[] mLevelHolder;
64 
MyMemFactorCallback(CountDownLatch[] latchHolder, int[] levelHolder)65         MyMemFactorCallback(CountDownLatch[] latchHolder, int[] levelHolder) {
66             mLatchHolder = latchHolder;
67             mLevelHolder = levelHolder;
68         }
69 
70         @Override
onTransact(int code, Parcel data, Parcel reply, int flags)71         protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
72                 throws RemoteException {
73             switch (code) {
74                 case COMMAND_TRIM_MEMORY_LEVEL:
75                     mLevelHolder[0] = data.readInt();
76                     mLatchHolder[0].countDown();
77                     return true;
78                 default:
79                     return false;
80             }
81         }
82     }
83 
84     private static class MyServiceConnection implements ServiceConnection {
85         private CountDownLatch mLatch;
86 
MyServiceConnection(CountDownLatch latch)87         MyServiceConnection(CountDownLatch latch) {
88             mLatch = latch;
89         }
90 
91         @Override
onServiceConnected(ComponentName name, IBinder service)92         public void onServiceConnected(ComponentName name, IBinder service) {
93             mLatch.countDown();
94         }
95 
96         @Override
onServiceDisconnected(ComponentName name)97         public void onServiceDisconnected(ComponentName name) {
98         }
99     }
100 
bindToTrimMemService(String packageName, String instanceName, CountDownLatch[] latchHolder, int[] levelHolder, Context context)101     public static ServiceConnection bindToTrimMemService(String packageName, String instanceName,
102             CountDownLatch[] latchHolder, int[] levelHolder, Context context) throws Exception {
103         final Intent intent = new Intent();
104         intent.setClassName(packageName, "android.app.stubs.TrimMemService");
105         final Bundle extras = new Bundle();
106         extras.putBinder(CommandReceiver.EXTRA_CALLBACK,
107                 new MyMemFactorCallback(latchHolder, levelHolder));
108         intent.putExtras(extras);
109         final CountDownLatch latch = new CountDownLatch(1);
110         final MyServiceConnection conn = new MyServiceConnection(latch);
111         context.bindIsolatedService(intent, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY,
112                 instanceName, AsyncTask.THREAD_POOL_EXECUTOR, conn);
113         latch.await();
114         return conn;
115     }
116 }
117 
118