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