summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/DynamicMemoryManagerService.java
blob: 5912461205826b53bf4ce3f6d4d178f7d6f9097e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Code Aurora nor
 *     the names of its contributors may be used to endorse or promote
 *     products derived from this software without specific prior written
 *     permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.server;

import android.os.Power;
import android.os.PowerManager;
import android.os.SystemProperties;

import android.app.AlarmManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.text.format.Time;

import java.util.Calendar;
import android.util.Log;

class DynamicMemoryManagerService {
    private final String TAG = "DMMService";
    private Context mContext;
    private boolean SCREEN_ON = false;
    private boolean DPD_START = false;
    private boolean START_ALARM_SET = false;
    private boolean STOP_ALARM_SET = false;
    private int prevStartHr = 0;
    private int prevStartMin = 0;
    private int prevStopHr = 0;
    private int prevStopMin = 0;
    private enum DMM_MEM_STATE {ACTIVE, DISABLED}
    private DMM_MEM_STATE mState;
    private AlarmManager mStartAlarmManager, mStopAlarmManager;
    private Intent mStartIntent, mStopIntent;
    private PendingIntent mStartPendingIntent, mStopPendingIntent;
    private static final String ACTION_DPD_START =
        "com.android.server.DMMService.action.DPD_START";
    private static final String ACTION_DPD_STOP =
        "com.android.server.DMMService.action.DPD_STOP";
    private PowerManager.WakeLock mWakeLock = null;
    private final static String WAKELOCK_NAME = "DynamicMemoryManagerServiceLock";
    private final boolean DEBUG = false;

    public DynamicMemoryManagerService(Context context) {
        mContext = context;

        Log.w(TAG, "ro.dev.dmm.dpd.start_address = "
                + SystemProperties.get("ro.dev.dmm.dpd.start_address", "0"));

        if(SystemProperties.get("ro.dev.dmm.dpd.start_address", "0").compareTo("0") != 0) {
                mState = DMM_MEM_STATE.ACTIVE;

                registerForBroadcasts();
                mStartIntent = new Intent(ACTION_DPD_START, null);
                mStopIntent = new Intent(ACTION_DPD_STOP, null);
                mStartPendingIntent = PendingIntent.getBroadcast(mContext, 0, mStartIntent, 0);
                mStopPendingIntent = PendingIntent.getBroadcast(mContext, 0, mStopIntent, 0);
                mStartAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
                mStopAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
                PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
                mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_NAME);
                Log.w(TAG, "DynamicMemoryManager Service Initialized");
        }
        else
            Log.w(TAG, "DynamicMemoryManager Service Disabled.");
    }

    private void registerForBroadcasts() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(ACTION_DPD_START);
        intentFilter.addAction(ACTION_DPD_STOP);
        mContext.registerReceiver(mReceiver, intentFilter);
    }

    protected void finalize() {
        if (DEBUG) Log.w(TAG, "DynamicMemoryManagerService Finalize");
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
                if (DEBUG) Log.w(TAG, "ACTION_BOOT_COMPLETED");
                manageAlarms();
            }
            else if(action.equals(Intent.ACTION_SCREEN_ON)) {
                if (DEBUG) Log.w(TAG, "ACTION_SCREEN_ON");
                enableUnstableMemory(true);
                SCREEN_ON = true;
            }
            else if(action.equals(Intent.ACTION_SCREEN_OFF)) {
                if (DEBUG) Log.w(TAG, "ACTION_SCREEN_OFF");
                SCREEN_ON = false;

                manageAlarms();
                checkCurrentStatus();
                if (DEBUG) Log.w(TAG, "DPD_START is set to " + DPD_START);

                if(DPD_START)
                    enableUnstableMemory(false);
            }
            else if (action.equals(ACTION_DPD_START)) {
                if (DEBUG) Log.w(TAG, "ACTION_DPD_START");
                DPD_START = true;
                START_ALARM_SET = false;
                if(!SCREEN_ON)
                    enableUnstableMemory(false);
            }
            else if (action.equals(ACTION_DPD_STOP)) {
                if (DEBUG) Log.w(TAG, "ACTION_DPD_STOP");
                DPD_START = false;
                STOP_ALARM_SET = false;
            }
        }
    };

    private boolean manageAlarms() {
        if (DEBUG) Log.w(TAG, "Manage Alarms");

        if(dpdEnabled() == 1) {
            setStartAlarm();
            setStopAlarm();
        }
        else {
            cancelStartAlarm();
            cancelStopAlarm();
            DPD_START = false;
        }
        return true;
    }

    private int dpdEnabled() {
        if (DEBUG) Log.w(TAG, "DPD Enabled: " +
                SystemProperties.getInt("persist.sys.dpd", 0));
        return SystemProperties.getInt("persist.sys.dpd", 0);
    }

    private boolean setStartAlarm() {
        int startHr = SystemProperties.getInt("persist.sys.dpd.start_hr", 20);
        int startMin = SystemProperties.getInt("persist.sys.dpd.start_min", 0);
        long delay_ms = 0;

        if (DEBUG) Log.w(TAG, "DPD_START = " + DPD_START);
        if (DEBUG) Log.w(TAG, "Prev Start: Hr = " + prevStartHr + " Min = " + prevStartMin);
        if (DEBUG) Log.w(TAG, "     Start: Hr = " + startHr + " Min = " + startMin);
        if (DEBUG) Log.w(TAG, "START_ALARM_SET = " + START_ALARM_SET);

        if(startHr != prevStartHr || startMin != prevStartMin || !START_ALARM_SET) {
            prevStartHr = startHr;
            prevStartMin = startMin;
            cancelStartAlarm();
            delay_ms = getDelay(startHr, startMin);
            if (DEBUG) Log.w(TAG, "Set Start Alarm after " + delay_ms + "ms");
            mStartAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                                            delay_ms,
                                            AlarmManager.INTERVAL_DAY,
                                            mStartPendingIntent);
            START_ALARM_SET = true;
        }
        return true;
    }

    private boolean cancelStartAlarm() {
        if (DEBUG) Log.w(TAG, "Cancel Start Alarm");
        mStartAlarmManager.cancel(mStartPendingIntent);
        START_ALARM_SET = false;
        return true;
    }

    private boolean setStopAlarm() {
        int stopHr = SystemProperties.getInt("persist.sys.dpd.stop_hr", 7);
        int stopMin = SystemProperties.getInt("persist.sys.dpd.stop_min", 0);
        long delay_ms = 0;

        if (DEBUG) Log.w(TAG, "Prev Stop Hr = " + prevStopHr + " Min = " + prevStopMin);
        if (DEBUG) Log.w(TAG, "     Stop Hr = " + stopHr + " Min = " + stopMin);
        if (DEBUG) Log.w(TAG, "STOP_ALARM_SET = " + STOP_ALARM_SET);

        if(stopHr != prevStopHr || stopMin != prevStopMin || !STOP_ALARM_SET) {
            prevStopHr = stopHr;
            prevStopMin = stopMin;
            cancelStopAlarm();
            delay_ms = getDelay(stopHr, stopMin);
            if (DEBUG) Log.w(TAG, "Set Stop Alarm after " + delay_ms + "ms");
            mStopAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                                           delay_ms,
                                           AlarmManager.INTERVAL_DAY,
                                           mStopPendingIntent);
            STOP_ALARM_SET = true;
        }
        return true;
    }

    private boolean cancelStopAlarm() {
        if (DEBUG) Log.w(TAG, "Cancel Stop Alarm");
        mStopAlarmManager.cancel(mStopPendingIntent);
        STOP_ALARM_SET = false;
        return true;
    }

    private long getDelay(int sHr, int sMin) {
        final Calendar calendar = Calendar.getInstance();
        int dHr = sHr - calendar.get(Calendar.HOUR_OF_DAY);
        int dMin = sMin - calendar.get(Calendar.MINUTE);

        if(dHr < 0)
            dHr = 24 + dHr;
        if(dMin < 0) {
            dMin = 60 + dMin;
            if(dHr > 0)
                dHr = dHr - 1;
            else
                dHr = 24 + dHr - 1;
        }
        if (DEBUG) Log.w(TAG, "Current time " + calendar.get(Calendar.HOUR_OF_DAY) +
                    ":" +calendar.get(Calendar.MINUTE));
        if (DEBUG) Log.w(TAG, "Alarm time " + sHr + ":" + sMin);
        if (DEBUG) Log.w(TAG, "Next Alarm after " + dHr + " hrs and " + dMin + " mins");

        return (((dHr * 60) + dMin) * 60 * 1000) + System.currentTimeMillis();
    }

    private void checkCurrentStatus() {
        final Calendar calendar = Calendar.getInstance();
        int StopDayOfMonth = 0;

        if(dpdEnabled() == 1) {
            StopDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            if((prevStartHr > prevStopHr) ||
               ((prevStartHr == prevStopHr) && (prevStartMin > prevStopMin)))
                StopDayOfMonth = StopDayOfMonth + 1;

            Time NowTime = new Time();
            NowTime.setToNow();

            Time StartTime = new Time();
            StartTime.set(calendar.get(Calendar.SECOND),
                            prevStartMin,
                            prevStartHr,
                            calendar.get(Calendar.DAY_OF_MONTH),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.YEAR));

            Time StopTime = new Time();
            StopTime.set(calendar.get(Calendar.SECOND),
                            prevStopMin,
                            prevStopHr,
                            StopDayOfMonth,
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.YEAR));

            if(NowTime.after(StartTime) && NowTime.before(StopTime))
                DPD_START = true;
            else
                DPD_START = false;
        }
        else {
            DPD_START = false;
        }
    }

    private int enableUnstableMemory(boolean flag) {
        Log.w(TAG, "Enable Unstable Memory : " + flag);
        synchronized (mWakeLock) {
            try {
                mWakeLock.acquire();
                if (DEBUG) Log.w(TAG, "Wakelock " + WAKELOCK_NAME + " Acquired");
            }
            catch (Exception e) {
                Log.e(TAG, "Unable to Acquired Wakelock: " + WAKELOCK_NAME);
            }
            if (flag) {
                if(mState == DMM_MEM_STATE.DISABLED) {
                    if(Power.SetUnstableMemoryState(flag) < 0)
                        Log.e(TAG, "Activating Unstable Memory: Failed !");
                    else
                        mState = DMM_MEM_STATE.ACTIVE;
                }
            } else {
                if((mState == DMM_MEM_STATE.ACTIVE) && (dpdEnabled() == 1)) {
                    if(Power.SetUnstableMemoryState(flag) < 0)
                        Log.e(TAG, "Disabling Unstable Memory: Failed !");
                    else
                            mState = DMM_MEM_STATE.DISABLED;
                }
            }
            try {
                if (mWakeLock.isHeld()) {
                    mWakeLock.release();
                    if (DEBUG) Log.w(TAG, "Wakelock " + WAKELOCK_NAME + " Released");
                }
                else {
                    Log.e(TAG, "Error: Attempring to release unheld Wakelock: " + WAKELOCK_NAME);
                }
            }
            catch (Exception e) {
                Log.e(TAG, "Unable to release Wakelock: " + WAKELOCK_NAME);
            }
        }
        return 0;
    }
}