Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c5a7cee authored by Matthew Xie's avatar Matthew Xie Committed by Android Git Automerger
Browse files

am b7b25659: DO NOT MERGE Move wake alarm setting and releasing into AdapterService\'s handler

* commit 'b7b25659':
  DO NOT MERGE Move wake alarm setting and releasing into AdapterService's handler
parents d75ac996 b7b25659
Loading
Loading
Loading
Loading
+34 −18
Original line number Diff line number Diff line
@@ -509,6 +509,8 @@ public class AdapterService extends Service {
    private static final int MESSAGE_PROFILE_CONNECTION_STATE_CHANGED=20;
    private static final int MESSAGE_CONNECT_OTHER_PROFILES = 30;
    private static final int MESSAGE_PROFILE_INIT_PRIORITIES=40;
    private static final int MESSAGE_SET_WAKE_ALARM = 100;
    private static final int MESSAGE_RELEASE_WAKE_ALARM = 110;
    private static final int CONNECT_OTHER_PROFILES_TIMEOUT= 6000;

    private final Handler mHandler = new Handler() {
@@ -542,6 +544,17 @@ public class AdapterService extends Service {
                    processConnectOtherProfiles((BluetoothDevice) msg.obj,msg.arg1);
                }
                    break;
                case MESSAGE_SET_WAKE_ALARM: {
                    debugLog( "handleMessage() - MESSAGE_SET_WAKE_ALARM");
                    processSetWakeAlarm((Long) msg.obj, msg.arg1);
                }
                    break;
                case MESSAGE_RELEASE_WAKE_ALARM: {
                    debugLog( "handleMessage() - MESSAGE_RELEASE_WAKE_ALARM");
                    mPendingAlarm = null;
                    alarmFiredNative();
                }
                    break;
            }
        }
    };
@@ -1755,24 +1768,30 @@ public class AdapterService extends Service {
    }

    // This function is called from JNI. It allows native code to set a single wake
    // alarm. If an alarm is already pending and a new request comes in, the alarm
    // will be rescheduled (i.e. the previously set alarm will be cancelled).
    // alarm.
    private boolean setWakeAlarm(long delayMillis, boolean shouldWake) {
        synchronized (this) {
        Message m = mHandler.obtainMessage(MESSAGE_SET_WAKE_ALARM);
        m.obj = new Long(delayMillis);
        // alarm type
        m.arg1 = shouldWake ? AlarmManager.ELAPSED_REALTIME_WAKEUP
            : AlarmManager.ELAPSED_REALTIME;
        mHandler.sendMessage(m);

        return true;
    }

    // If an alarm is already pending and a new request comes in, the alarm
    // will be rescheduled (i.e. the previously set alarm will be cancelled).
    private void processSetWakeAlarm(long delayMillis, int alarmType) {
        if (mPendingAlarm != null) {
            mAlarmManager.cancel(mPendingAlarm);
        }

        long wakeupTime = SystemClock.elapsedRealtime() + delayMillis;
            int type = shouldWake
                ? AlarmManager.ELAPSED_REALTIME_WAKEUP
                : AlarmManager.ELAPSED_REALTIME;

        Intent intent = new Intent(ACTION_ALARM_WAKEUP);
        mPendingAlarm = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            mAlarmManager.setExact(type, wakeupTime, mPendingAlarm);
            return true;
        }
        mAlarmManager.setExact(alarmType, wakeupTime, mPendingAlarm);
    }

    // This function is called from JNI. It allows native code to acquire a single wake lock.
@@ -1850,10 +1869,7 @@ public class AdapterService extends Service {
    private final BroadcastReceiver mAlarmBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (AdapterService.this) {
                mPendingAlarm = null;
                alarmFiredNative();
            }
            mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_RELEASE_WAKE_ALARM));
        }
    };