Use AlarmManager calls to set date/time, instead of SystemClock.setTime

This way secondary users' settings app can request the system server to
set the time. Alarm driver cannot be opened as a secondary user.

Bug: 7459635
Change-Id: I1ae1630dc448021d35280a297c5d9960f8e8fc2e
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index f2e51a1..71cc520 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -61,6 +61,7 @@
     <uses-permission android:name="android.permission.MANAGE_USERS" />
     <uses-permission android:name="android.permission.READ_PROFILE" />
     <uses-permission android:name="android.permission.CONFIGURE_WIFI_DISPLAY" />
+    <uses-permission android:name="android.permission.SET_TIME" />
 
     <application android:label="@string/settings_label"
             android:icon="@mipmap/ic_launcher_settings"
diff --git a/src/com/android/settings/DateTimeSettings.java b/src/com/android/settings/DateTimeSettings.java
index 53f25a2..65a34f4 100644
--- a/src/com/android/settings/DateTimeSettings.java
+++ b/src/com/android/settings/DateTimeSettings.java
@@ -17,6 +17,7 @@
 package com.android.settings;
 
 import android.app.Activity;
+import android.app.AlarmManager;
 import android.app.DatePickerDialog;
 import android.app.Dialog;
 import android.app.TimePickerDialog;
@@ -184,18 +185,18 @@
 
     @Override
     public void onDateSet(DatePicker view, int year, int month, int day) {
-        setDate(year, month, day);
         final Activity activity = getActivity();
         if (activity != null) {
+            setDate(activity, year, month, day);
             updateTimeAndDateDisplay(activity);
         }
     }
 
     @Override
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
-        setTime(hourOfDay, minute);
         final Activity activity = getActivity();
         if (activity != null) {
+            setTime(activity, hourOfDay, minute);
             updateTimeAndDateDisplay(activity);
         }
 
@@ -337,7 +338,7 @@
         }
     }
 
-    /* package */ static void setDate(int year, int month, int day) {
+    /* package */ static void setDate(Context context, int year, int month, int day) {
         Calendar c = Calendar.getInstance();
 
         c.set(Calendar.YEAR, year);
@@ -346,11 +347,11 @@
         long when = c.getTimeInMillis();
 
         if (when / 1000 < Integer.MAX_VALUE) {
-            SystemClock.setCurrentTimeMillis(when);
+            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
         }
     }
 
-    /* package */ static void setTime(int hourOfDay, int minute) {
+    /* package */ static void setTime(Context context, int hourOfDay, int minute) {
         Calendar c = Calendar.getInstance();
 
         c.set(Calendar.HOUR_OF_DAY, hourOfDay);
@@ -360,7 +361,7 @@
         long when = c.getTimeInMillis();
 
         if (when / 1000 < Integer.MAX_VALUE) {
-            SystemClock.setCurrentTimeMillis(when);
+            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
         }
     }
 
diff --git a/src/com/android/settings/DateTimeSettingsSetupWizard.java b/src/com/android/settings/DateTimeSettingsSetupWizard.java
index 87b1cae..05e0d4e 100644
--- a/src/com/android/settings/DateTimeSettingsSetupWizard.java
+++ b/src/com/android/settings/DateTimeSettingsSetupWizard.java
@@ -188,9 +188,9 @@
                 Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME,
                       mAutoDateTimeButton.isChecked() ? 1 : 0);
                 if (!mAutoDateTimeButton.isChecked()) {
-                    DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
+                    DateTimeSettings.setDate(this, mDatePicker.getYear(), mDatePicker.getMonth(),
                             mDatePicker.getDayOfMonth());
-                    DateTimeSettings.setTime(
+                    DateTimeSettings.setTime(this,
                             mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
                 }
             }