• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.datetime.timezone.model;
18 
19 import android.app.LoaderManager;
20 import android.content.Context;
21 import android.content.Loader;
22 import android.os.Bundle;
23 
24 import com.android.settingslib.utils.AsyncLoader;
25 
26 public class TimeZoneDataLoader extends AsyncLoader<TimeZoneData> {
27 
TimeZoneDataLoader(Context context)28     public TimeZoneDataLoader(Context context) {
29         super(context);
30     }
31 
32     @Override
loadInBackground()33     public TimeZoneData loadInBackground() {
34         // Heavy operation due to reading the underlying file
35         return TimeZoneData.getInstance();
36     }
37 
38     @Override
onDiscardResult(TimeZoneData result)39     protected void onDiscardResult(TimeZoneData result) {
40         // This class doesn't hold resource of the result.
41     }
42 
43     public interface OnDataReadyCallback {
onTimeZoneDataReady(TimeZoneData data)44         void onTimeZoneDataReady(TimeZoneData data);
45     }
46 
47     public static class LoaderCreator implements LoaderManager.LoaderCallbacks<TimeZoneData> {
48 
49         private final Context mContext;
50         private final OnDataReadyCallback mCallback;
51 
LoaderCreator(Context context, OnDataReadyCallback callback)52         public LoaderCreator(Context context, OnDataReadyCallback callback) {
53             mContext = context;
54             mCallback = callback;
55         }
56 
57         @Override
onCreateLoader(int id, Bundle args)58         public Loader onCreateLoader(int id, Bundle args) {
59             return new TimeZoneDataLoader(mContext);
60         }
61 
62         @Override
onLoadFinished(Loader<TimeZoneData> loader, TimeZoneData data)63         public void onLoadFinished(Loader<TimeZoneData> loader, TimeZoneData data) {
64             if (mCallback != null) {
65                 mCallback.onTimeZoneDataReady(data);
66             }
67         }
68 
69         @Override
onLoaderReset(Loader<TimeZoneData> loader)70         public void onLoaderReset(Loader<TimeZoneData> loader) {
71             //It's okay to keep the time zone data when loader is reset
72         }
73     }
74 }
75