Skip to content

Commit

Permalink
[TF][KILL] remove fusedlocationprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
thermatk committed Jul 27, 2019
1 parent f440ac6 commit 1f505ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.telegram.messenger;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
Expand All @@ -22,17 +23,6 @@
import android.util.LongSparseArray;
import android.util.SparseIntArray;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;

import org.telegram.SQLite.SQLiteCursor;
import org.telegram.SQLite.SQLitePreparedStatement;
import org.telegram.tgnet.NativeByteBuffer;
Expand All @@ -43,7 +33,7 @@
import java.util.List;
import java.util.Locale;

public class LocationController extends BaseController implements NotificationCenter.NotificationCenterDelegate, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public class LocationController extends BaseController implements NotificationCenter.NotificationCenterDelegate {

private LongSparseArray<SharingLocationInfo> sharingLocationsMap = new LongSparseArray<>();
private ArrayList<SharingLocationInfo> sharingLocations = new ArrayList<>();
Expand All @@ -52,7 +42,6 @@ public class LocationController extends BaseController implements NotificationCe
private GpsLocationListener gpsLocationListener = new GpsLocationListener();
private GpsLocationListener networkLocationListener = new GpsLocationListener();
private GpsLocationListener passiveLocationListener = new GpsLocationListener();
private FusedLocationListener fusedLocationListener = new FusedLocationListener();
private Location lastKnownLocation;
private long lastLocationSendTime;
private boolean locationSentSinceLastGoogleMapUpdate = true;
Expand All @@ -67,10 +56,6 @@ public class LocationController extends BaseController implements NotificationCe
public ArrayList<SharingLocationInfo> sharingLocationsUI = new ArrayList<>();
private LongSparseArray<SharingLocationInfo> sharingLocationsMapUI = new LongSparseArray<>();

private Boolean playServicesAvailable;
private boolean wasConnectedToPlayServices;
private GoogleApiClient googleApiClient;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private final static long UPDATE_INTERVAL = 1000, FASTEST_INTERVAL = 1000;
private final static int BACKGROUD_UPDATE_TIME = 30 * 1000;
private final static int LOCATION_ACQUIRE_TIME = 10 * 1000;
Expand All @@ -79,8 +64,6 @@ public class LocationController extends BaseController implements NotificationCe
private ArrayList<TLRPC.TL_peerLocated> cachedNearbyUsers = new ArrayList<>();
private ArrayList<TLRPC.TL_peerLocated> cachedNearbyChats = new ArrayList<>();

private LocationRequest locationRequest;

private static volatile LocationController[] Instance = new LocationController[UserConfig.MAX_ACCOUNT_COUNT];

public static LocationController getInstance(int num) {
Expand Down Expand Up @@ -137,30 +120,10 @@ public void onProviderDisabled(String provider) {
}
}

private class FusedLocationListener implements com.google.android.gms.location.LocationListener {

@Override
public void onLocationChanged(Location location) {
if (location == null) {
return;
}
setLastKnownLocation(location);
}
}

public LocationController(int instance) {
super(instance);

locationManager = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
googleApiClient = new GoogleApiClient.Builder(ApplicationLoader.applicationContext).
addApi(LocationServices.API).
addConnectionCallbacks(this).
addOnConnectionFailedListener(this).build();

locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);

AndroidUtilities.runOnUIThread(() -> {
LocationController locationController = getAccountInstance().getLocationController();
Expand Down Expand Up @@ -260,89 +223,6 @@ public void didReceivedNotification(int id, int account, Object... args) {
}
}

@Override
public void onConnected(Bundle bundle) {
wasConnectedToPlayServices = true;
try {
if (Build.VERSION.SDK_INT >= 21) {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(locationSettingsResult -> {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
startFusedLocationRequest(true);
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.needShowPlayServicesAlert, status));
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Utilities.stageQueue.postRunnable(() -> {
playServicesAvailable = false;
try {
googleApiClient.disconnect();
start();
} catch (Throwable ignore) {

}
});
break;
}
});
} else {
startFusedLocationRequest(true);
}
} catch (Throwable e) {
FileLog.e(e);
}
}

public void startFusedLocationRequest(boolean permissionsGranted) {
Utilities.stageQueue.postRunnable(() -> {
if (!permissionsGranted) {
playServicesAvailable = false;
}
if (lookingForPeopleNearby || !sharingLocations.isEmpty()) {
if (permissionsGranted) {
try {
setLastKnownLocation(LocationServices.FusedLocationApi.getLastLocation(googleApiClient));
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, fusedLocationListener);
} catch (Throwable e) {
FileLog.e(e);
}
} else {
start();
}
}
});
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (wasConnectedToPlayServices) {
return;
}
playServicesAvailable = false;
if (started) {
started = false;
start();
}
}

private boolean checkPlayServices() {
if (playServicesAvailable == null) {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(ApplicationLoader.applicationContext);
playServicesAvailable = resultCode == ConnectionResult.SUCCESS;
}
return playServicesAvailable;
}

private void broadcastLastKnownLocation() {
if (lastKnownLocation == null) {
return;
Expand Down Expand Up @@ -734,21 +614,15 @@ public void setGoogleMapLocation(Location location, boolean first) {
setLastKnownLocation(location);
}

// TFOSS it asks properly anyway
@SuppressLint("MissingPermission")
private void start() {
if (started) {
return;
}
lastLocationStartTime = SystemClock.uptimeMillis();
started = true;
boolean ok = false;
if (checkPlayServices()) {
try {
googleApiClient.connect();
ok = true;
} catch (Throwable e) {
FileLog.e(e);
}
}
if (!ok) {
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, gpsLocationListener);
Expand Down Expand Up @@ -783,14 +657,6 @@ private void stop(boolean empty) {
return;
}
started = false;
if (checkPlayServices()) {
try {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, fusedLocationListener);
googleApiClient.disconnect();
} catch (Throwable e) {
FileLog.e(e);
}
}
locationManager.removeUpdates(gpsLocationListener);
if (empty) {
locationManager.removeUpdates(networkLocationListener);
Expand Down
43 changes: 14 additions & 29 deletions TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.common.api.Status;

import org.telegram.messenger.AccountInstance;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.BuildVars;
Expand Down Expand Up @@ -158,8 +156,6 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa

private Runnable lockRunnable;

private static final int PLAY_SERVICES_REQUEST_CHECK_SETTINGS = 140;

@Override
protected void onCreate(Bundle savedInstanceState) {
ApplicationLoader.postInitApplication();
Expand Down Expand Up @@ -2362,26 +2358,22 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
UserConfig.getInstance(currentAccount).saveConfig(false);
}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLAY_SERVICES_REQUEST_CHECK_SETTINGS) {
LocationController.getInstance(currentAccount).startFusedLocationRequest(resultCode == Activity.RESULT_OK);
} else {
ThemeEditorView editorView = ThemeEditorView.getInstance();
if (editorView != null) {
editorView.onActivityResult(requestCode, resultCode, data);
}
if (actionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = actionBarLayout.fragmentsStack.get(actionBarLayout.fragmentsStack.size() - 1);
ThemeEditorView editorView = ThemeEditorView.getInstance();
if (editorView != null) {
editorView.onActivityResult(requestCode, resultCode, data);
}
if (actionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = actionBarLayout.fragmentsStack.get(actionBarLayout.fragmentsStack.size() - 1);
fragment.onActivityResultFragment(requestCode, resultCode, data);
}
if (AndroidUtilities.isTablet()) {
if (rightActionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = rightActionBarLayout.fragmentsStack.get(rightActionBarLayout.fragmentsStack.size() - 1);
fragment.onActivityResultFragment(requestCode, resultCode, data);
}
if (AndroidUtilities.isTablet()) {
if (rightActionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = rightActionBarLayout.fragmentsStack.get(rightActionBarLayout.fragmentsStack.size() - 1);
fragment.onActivityResultFragment(requestCode, resultCode, data);
}
if (layersActionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = layersActionBarLayout.fragmentsStack.get(layersActionBarLayout.fragmentsStack.size() - 1);
fragment.onActivityResultFragment(requestCode, resultCode, data);
}
if (layersActionBarLayout.fragmentsStack.size() != 0) {
BaseFragment fragment = layersActionBarLayout.fragmentsStack.get(layersActionBarLayout.fragmentsStack.size() - 1);
fragment.onActivityResultFragment(requestCode, resultCode, data);
}
}
}
Expand Down Expand Up @@ -2802,13 +2794,6 @@ public void didReceivedNotification(int id, final int account, Object... args) {
}
}
}
} else if (id == NotificationCenter.needShowPlayServicesAlert) {
try {
final Status status = (Status) args[0];
status.startResolutionForResult(this, PLAY_SERVICES_REQUEST_CHECK_SETTINGS);
} catch (Throwable ignore) {

}
}
}

Expand Down

0 comments on commit 1f505ff

Please sign in to comment.