1 /* 2 * Copyright (C) 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.timezone.location.provider; 18 19 import android.content.Context; 20 import android.service.timezone.TimeZoneProviderService; 21 22 import androidx.annotation.NonNull; 23 24 import com.android.timezone.location.provider.core.Environment; 25 import com.android.timezone.location.provider.core.OfflineLocationTimeZoneDelegate; 26 import com.android.timezone.location.provider.core.TimeZoneProviderResult; 27 28 import java.io.FileDescriptor; 29 import java.io.PrintWriter; 30 import java.time.Duration; 31 32 /** 33 * A Location Time Zone Provider implementation that uses only Android public SDK APIs and on-device 34 * data to determine the current time zone(s) for a location. 35 * 36 * <p>This implementation can be deployed to run as either the current user (e.g. it could run in a 37 * user-specific process like GMS core), or always as the system user (e.g. it can run in a single 38 * user process like system server). It relies on being stopped by the system server when the user 39 * changes. No location state is retained when the provider is stopped. 40 * 41 * <p>See {@link OfflineLocationTimeZoneDelegate} for implementation details. 42 * 43 * <p>The provider is configured via a "offlineltzprovider.properties" resource file. See 44 * {@link EnvironmentImpl} for details. 45 */ 46 public final class OfflineLocationTimeZoneProviderService extends TimeZoneProviderService { 47 48 private static final String ATTRIBUTION_TAG = "OfflineLocationTimeZoneProviderService"; 49 50 // NonNull after onCreate() 51 private OfflineLocationTimeZoneDelegate mDelegate; 52 53 @Override onCreate()54 public void onCreate() { 55 Context attributionContext = createAttributionContext(ATTRIBUTION_TAG); 56 Environment environment = new EnvironmentImpl( 57 attributionContext, this::reportTimeZoneProviderEvent); 58 mDelegate = OfflineLocationTimeZoneDelegate.create(environment); 59 } 60 61 @Override onStartUpdates(long initializationTimeoutMillis)62 public void onStartUpdates(long initializationTimeoutMillis) { 63 mDelegate.onStartUpdates(Duration.ofMillis(initializationTimeoutMillis)); 64 } 65 reportTimeZoneProviderEvent( @onNull TimeZoneProviderResult timeZoneProviderResult)66 private void reportTimeZoneProviderEvent( 67 @NonNull TimeZoneProviderResult timeZoneProviderResult) { 68 switch (timeZoneProviderResult.getType()) { 69 case TimeZoneProviderResult.RESULT_TYPE_SUGGESTION: { 70 reportSuggestion(timeZoneProviderResult.getSuggestion()); 71 break; 72 } 73 case TimeZoneProviderResult.RESULT_TYPE_UNCERTAIN: { 74 reportUncertain(); 75 break; 76 } 77 case TimeZoneProviderResult.RESULT_TYPE_PERMANENT_FAILURE: { 78 reportPermanentFailure(timeZoneProviderResult.getFailureCause()); 79 break; 80 } 81 default: { 82 throw new IllegalArgumentException("Unknown result type=" + timeZoneProviderResult); 83 } 84 } 85 } 86 87 @Override onStopUpdates()88 public void onStopUpdates() { 89 mDelegate.onStopUpdates(); 90 } 91 92 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)93 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 94 mDelegate.dump(writer); 95 } 96 }