1 /*
2  * Copyright (C) 2006 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 android.telephony;
18 
19 import android.app.ActivityThread;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
25 import android.telephony.cdma.CdmaCellLocation;
26 import android.telephony.gsm.GsmCellLocation;
27 
28 import com.android.internal.telephony.ITelephony;
29 import com.android.internal.telephony.PhoneConstants;
30 
31 /**
32  * Abstract class that represents the location of the device.  {@more}
33  */
34 public abstract class CellLocation {
35 
36     /**
37      * Request an updated CellLocation for callers targeting SDK 30 or older.
38      *
39      * Whenever Android is aware of location changes, a callback will automatically be sent to
40      * all registrants of {@link PhoneStateListener#LISTEN_CELL_LOCATION}. This API requests an
41      * additional location update for cases where power saving might cause location updates to be
42      * missed.
43      *
44      * <p>This method is a no-op for callers targeting SDK level 31 or greater.
45      * <p>This method is a no-op for callers that target SDK level 29 or 30 and lack
46      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
47      * <p>This method is a no-op for callers that target SDK level 28 or below and lack
48      * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
49      *
50      * @deprecated use {@link TelephonyManager#requestCellInfoUpdate}.
51      */
52     @Deprecated
requestLocationUpdate()53     public static void requestLocationUpdate() {
54         // Since this object doesn't have a context, this is the best we can do.
55         final Context appContext = ActivityThread.currentApplication();
56         if (appContext == null) return; // should never happen
57 
58         try {
59             ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.getService("phone"));
60             if (phone != null) {
61                 phone.updateServiceLocationWithPackageName(appContext.getOpPackageName());
62             }
63         } catch (RemoteException ex) {
64             // ignore it
65         }
66     }
67 
68     /**
69      * Create a new CellLocation from a intent notifier Bundle
70      *
71      * This method is used by PhoneStateIntentReceiver and maybe by
72      * external applications.
73      *
74      * @param bundle Bundle from intent notifier
75      * @return newly created CellLocation
76      *
77      * @hide
78      */
79     @UnsupportedAppUsage
newFromBundle(Bundle bundle)80     public static CellLocation newFromBundle(Bundle bundle) {
81         // TelephonyManager.getDefault().getCurrentPhoneType() handles the case when
82         // ITelephony interface is not up yet.
83         switch(TelephonyManager.getDefault().getCurrentPhoneType()) {
84         case PhoneConstants.PHONE_TYPE_CDMA:
85             return new CdmaCellLocation(bundle);
86         case PhoneConstants.PHONE_TYPE_GSM:
87             return new GsmCellLocation(bundle);
88         default:
89             return null;
90         }
91     }
92 
93     /**
94      * @hide
95      */
96     @UnsupportedAppUsage
fillInNotifierBundle(Bundle bundle)97     public abstract void fillInNotifierBundle(Bundle bundle);
98 
99     /**
100      * @hide
101      */
102     @UnsupportedAppUsage
isEmpty()103     public abstract boolean isEmpty();
104 
105     /**
106      * Invalidate this object.  The location area code and the cell id are set to -1.
107      * @hide
108      */
setStateInvalid()109     public abstract void setStateInvalid();
110 
111     /**
112      * Return a new CellLocation object representing an unknown
113      * location, or null for unknown/none phone radio types.
114      *
115      */
getEmpty()116     public static CellLocation getEmpty() {
117         // TelephonyManager.getDefault().getCurrentPhoneType() handles the case when
118         // ITelephony interface is not up yet.
119         switch(TelephonyManager.getDefault().getCurrentPhoneType()) {
120         case PhoneConstants.PHONE_TYPE_CDMA:
121             return new CdmaCellLocation();
122         case PhoneConstants.PHONE_TYPE_GSM:
123             return new GsmCellLocation();
124         default:
125             return null;
126         }
127     }
128 }
129