• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 com.android.deskclock;
18 
19 import android.annotation.TargetApi;
20 import android.app.Application;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.os.Build;
24 import android.preference.PreferenceManager;
25 
26 import com.android.deskclock.controller.Controller;
27 import com.android.deskclock.data.DataModel;
28 import com.android.deskclock.events.LogEventTracker;
29 import com.android.deskclock.uidata.UiDataModel;
30 
31 public class DeskClockApplication extends Application {
32 
33     @Override
onCreate()34     public void onCreate() {
35         super.onCreate();
36 
37         final Context applicationContext = getApplicationContext();
38         final SharedPreferences prefs = getDefaultSharedPreferences(applicationContext);
39 
40         DataModel.getDataModel().init(applicationContext, prefs);
41         UiDataModel.getUiDataModel().init(applicationContext, prefs);
42         Controller.getController().setContext(applicationContext);
43         Controller.getController().addEventTracker(new LogEventTracker(applicationContext));
44     }
45 
46     /**
47      * Returns the default {@link SharedPreferences} instance from the underlying storage context.
48      */
49     @TargetApi(Build.VERSION_CODES.N)
getDefaultSharedPreferences(Context context)50     private static SharedPreferences getDefaultSharedPreferences(Context context) {
51         final Context storageContext;
52         if (Utils.isNOrLater()) {
53             // All N devices have split storage areas. Migrate the existing preferences into the new
54             // device encrypted storage area if that has not yet occurred.
55             final String name = PreferenceManager.getDefaultSharedPreferencesName(context);
56             storageContext = context.createDeviceProtectedStorageContext();
57             if (!storageContext.moveSharedPreferencesFrom(context, name)) {
58                 LogUtils.wtf("Failed to migrate shared preferences");
59             }
60         } else {
61             storageContext = context;
62         }
63         return PreferenceManager.getDefaultSharedPreferences(storageContext);
64     }
65 }