1 /* 2 * Copyright (C) 2019 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 package com.android.customization.model.clock; 17 18 import android.content.ContentResolver; 19 import android.provider.Settings.Secure; 20 import android.text.TextUtils; 21 22 import com.android.customization.module.ThemesUserEventLogger; 23 24 import org.json.JSONException; 25 import org.json.JSONObject; 26 27 /** 28 * {@link CustomizationManager} for clock faces that implements apply by writing to secure settings. 29 */ 30 public class ClockManager extends BaseClockManager { 31 32 // TODO: use constant from Settings.Secure 33 static final String CLOCK_FACE_SETTING = "lock_screen_custom_clock_face"; 34 private static final String CLOCK_FIELD = "clock"; 35 private static final String TIMESTAMP_FIELD = "_applied_timestamp"; 36 private final ContentResolver mContentResolver; 37 private final ThemesUserEventLogger mEventLogger; 38 ClockManager(ContentResolver resolver, ClockProvider provider, ThemesUserEventLogger logger)39 public ClockManager(ContentResolver resolver, ClockProvider provider, 40 ThemesUserEventLogger logger) { 41 super(provider); 42 mContentResolver = resolver; 43 mEventLogger = logger; 44 } 45 46 @Override handleApply(Clockface option, Callback callback)47 protected void handleApply(Clockface option, Callback callback) { 48 boolean stored; 49 try { 50 final JSONObject json = new JSONObject(); 51 json.put(CLOCK_FIELD, option.getId()); 52 json.put(TIMESTAMP_FIELD, System.currentTimeMillis()); 53 stored = Secure.putString(mContentResolver, CLOCK_FACE_SETTING, json.toString()); 54 } catch (JSONException ex) { 55 stored = false; 56 } 57 if (stored) { 58 mEventLogger.logClockApplied(option); 59 callback.onSuccess(); 60 } else { 61 callback.onError(null); 62 } 63 } 64 65 @Override lookUpCurrentClock()66 protected String lookUpCurrentClock() { 67 final String value = Secure.getString(mContentResolver, CLOCK_FACE_SETTING); 68 if (TextUtils.isEmpty(value)) { 69 return value; 70 } 71 try { 72 final JSONObject json = new JSONObject(value); 73 return json.getString(CLOCK_FIELD); 74 } catch (JSONException ex) { 75 return value; 76 } 77 } 78 } 79