• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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.car.calendar;
18 
19 import android.content.ContentResolver;
20 import android.os.Handler;
21 import android.os.HandlerThread;
22 import android.telephony.TelephonyManager;
23 import android.util.Log;
24 
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.ViewModel;
27 
28 import com.android.car.calendar.common.EventDescriptions;
29 import com.android.car.calendar.common.EventLocations;
30 import com.android.car.calendar.common.EventsLiveData;
31 
32 import java.time.Clock;
33 import java.util.Locale;
34 
35 class CarCalendarViewModel extends ViewModel {
36     private static final String TAG = "CarCalendarViewModel";
37     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
38 
39     private final HandlerThread mHandlerThread = new HandlerThread("CarCalendarBackground");
40     private final Clock mClock;
41     private final ContentResolver mResolver;
42     private final Locale mLocale;
43     private final TelephonyManager mTelephonyManager;
44 
45     @Nullable private EventsLiveData mEventsLiveData;
46 
CarCalendarViewModel(ContentResolver resolver, Locale locale, TelephonyManager telephonyManager, Clock clock)47     CarCalendarViewModel(ContentResolver resolver, Locale locale,
48             TelephonyManager telephonyManager, Clock clock) {
49         mLocale = locale;
50         if (DEBUG) Log.d(TAG, "Creating view model");
51         mResolver = resolver;
52         mTelephonyManager = telephonyManager;
53         mHandlerThread.start();
54         mClock = clock;
55     }
56 
57     /** Creates an {@link EventsLiveData} lazily and always returns the same instance. */
getEventsLiveData()58     EventsLiveData getEventsLiveData() {
59         if (mEventsLiveData == null) {
60             mEventsLiveData =
61                     new EventsLiveData(
62                             mClock,
63                             new Handler(mHandlerThread.getLooper()),
64                             mResolver,
65                             new EventDescriptions(mLocale, mTelephonyManager),
66                             new EventLocations());
67         }
68         return mEventsLiveData;
69     }
70 
71     @Override
onCleared()72     protected void onCleared() {
73         super.onCleared();
74         mHandlerThread.quitSafely();
75     }
76 }
77