2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui;
|
|
|
|
|
2014-08-08 10:17:06 +00:00
|
|
|
import android.content.Context;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.location.Location;
|
2014-08-08 10:17:06 +00:00
|
|
|
import android.location.LocationManager;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.google.android.gms.maps.CameraUpdate;
|
|
|
|
import com.google.android.gms.maps.CameraUpdateFactory;
|
|
|
|
import com.google.android.gms.maps.GoogleMap;
|
2014-06-03 23:31:48 +00:00
|
|
|
import com.google.android.gms.maps.MapView;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
import com.google.android.gms.maps.MapsInitializer;
|
2013-10-25 15:19:00 +00:00
|
|
|
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
|
|
|
import com.google.android.gms.maps.model.LatLng;
|
|
|
|
import com.google.android.gms.maps.model.Marker;
|
|
|
|
import com.google.android.gms.maps.model.MarkerOptions;
|
2014-03-22 22:31:55 +00:00
|
|
|
|
2014-09-24 02:17:27 +00:00
|
|
|
import org.telegram.android.AndroidUtilities;
|
|
|
|
import org.telegram.android.ContactsController;
|
2014-06-03 23:31:48 +00:00
|
|
|
import org.telegram.messenger.FileLog;
|
2014-07-02 22:39:05 +00:00
|
|
|
import org.telegram.android.LocaleController;
|
2014-02-28 22:28:25 +00:00
|
|
|
import org.telegram.messenger.TLRPC;
|
2014-08-22 14:24:33 +00:00
|
|
|
import org.telegram.android.MessageObject;
|
2014-07-02 22:39:05 +00:00
|
|
|
import org.telegram.android.MessagesController;
|
2014-08-22 14:24:33 +00:00
|
|
|
import org.telegram.android.NotificationCenter;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.R;
|
2014-06-03 23:31:48 +00:00
|
|
|
import org.telegram.ui.Views.ActionBar.ActionBarLayer;
|
|
|
|
import org.telegram.ui.Views.ActionBar.ActionBarMenu;
|
|
|
|
import org.telegram.ui.Views.ActionBar.ActionBarMenuItem;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.ui.Views.BackupImageView;
|
2014-06-03 23:31:48 +00:00
|
|
|
import org.telegram.ui.Views.ActionBar.BaseFragment;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-08-08 10:17:06 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2013-12-26 16:46:13 +00:00
|
|
|
public class LocationActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
|
2013-10-25 15:19:00 +00:00
|
|
|
private GoogleMap googleMap;
|
|
|
|
private TextView distanceTextView;
|
|
|
|
private Marker userMarker;
|
|
|
|
private Location myLocation;
|
|
|
|
private Location userLocation;
|
|
|
|
private MessageObject messageObject;
|
|
|
|
private BackupImageView avatarImageView;
|
|
|
|
private TextView nameTextView;
|
|
|
|
private boolean userLocationMoved = false;
|
|
|
|
private boolean firstWas = false;
|
2014-06-03 23:31:48 +00:00
|
|
|
private MapView mapView;
|
2014-08-22 14:24:33 +00:00
|
|
|
private LocationActivityDelegate delegate;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-04-02 17:36:57 +00:00
|
|
|
private final static int map_to_my_location = 1;
|
|
|
|
private final static int map_list_menu_map = 2;
|
|
|
|
private final static int map_list_menu_satellite = 3;
|
|
|
|
private final static int map_list_menu_hybrid = 4;
|
|
|
|
|
2014-08-22 14:24:33 +00:00
|
|
|
public static interface LocationActivityDelegate {
|
|
|
|
public abstract void didSelectLocation(double latitude, double longitude);
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public boolean onFragmentCreate() {
|
|
|
|
super.onFragmentCreate();
|
2014-06-17 15:10:02 +00:00
|
|
|
swipeBackEnabled = false;
|
2014-08-22 14:24:33 +00:00
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (messageObject != null) {
|
2014-08-22 14:24:33 +00:00
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFragmentDestroy() {
|
|
|
|
super.onFragmentDestroy();
|
2014-08-22 14:24:33 +00:00
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateInterfaces);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.closeChats);
|
2014-06-03 23:31:48 +00:00
|
|
|
if (mapView != null) {
|
|
|
|
mapView.onDestroy();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-03 23:31:48 +00:00
|
|
|
public View createView(LayoutInflater inflater, ViewGroup container) {
|
|
|
|
if (fragmentView == null) {
|
2014-06-12 01:13:15 +00:00
|
|
|
actionBarLayer.setDisplayHomeAsUpEnabled(true, R.drawable.ic_ab_back);
|
2014-06-12 15:53:20 +00:00
|
|
|
actionBarLayer.setBackOverlay(R.layout.updating_state_layout);
|
2014-06-03 23:31:48 +00:00
|
|
|
if (messageObject != null) {
|
|
|
|
actionBarLayer.setTitle(LocaleController.getString("ChatLocation", R.string.ChatLocation));
|
|
|
|
} else {
|
|
|
|
actionBarLayer.setTitle(LocaleController.getString("ShareLocation", R.string.ShareLocation));
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
actionBarLayer.setActionBarMenuOnItemClick(new ActionBarLayer.ActionBarMenuOnItemClick() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(int id) {
|
|
|
|
if (id == -1) {
|
|
|
|
finishFragment();
|
|
|
|
} else if (id == map_list_menu_map) {
|
|
|
|
if (googleMap != null) {
|
|
|
|
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
} else if (id == map_list_menu_satellite) {
|
|
|
|
if (googleMap != null) {
|
|
|
|
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
|
|
|
|
}
|
|
|
|
} else if (id == map_list_menu_hybrid) {
|
|
|
|
if (googleMap != null) {
|
|
|
|
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
|
|
|
|
}
|
|
|
|
} else if (id == map_to_my_location) {
|
|
|
|
if (myLocation != null) {
|
|
|
|
LatLng latLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
|
|
|
|
if (googleMap != null) {
|
|
|
|
CameraUpdate position = CameraUpdateFactory.newLatLngZoom(latLng, googleMap.getMaxZoomLevel() - 8);
|
|
|
|
googleMap.animateCamera(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
ActionBarMenu menu = actionBarLayer.createMenu();
|
|
|
|
menu.addItem(map_to_my_location, R.drawable.ic_ab_location);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
ActionBarMenuItem item = menu.addItem(0, R.drawable.ic_ab_other);
|
|
|
|
item.addSubItem(map_list_menu_map, LocaleController.getString("Map", R.string.Map), 0);
|
|
|
|
item.addSubItem(map_list_menu_satellite, LocaleController.getString("Satellite", R.string.Satellite), 0);
|
|
|
|
item.addSubItem(map_list_menu_hybrid, LocaleController.getString("Hybrid", R.string.Hybrid), 0);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
if (messageObject != null) {
|
|
|
|
fragmentView = inflater.inflate(R.layout.location_view_layout, container, false);
|
|
|
|
} else {
|
|
|
|
fragmentView = inflater.inflate(R.layout.location_attach_layout, container, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
avatarImageView = (BackupImageView)fragmentView.findViewById(R.id.location_avatar_view);
|
2014-07-19 23:31:49 +00:00
|
|
|
if (avatarImageView != null) {
|
|
|
|
avatarImageView.processDetach = false;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
nameTextView = (TextView)fragmentView.findViewById(R.id.location_name_label);
|
|
|
|
distanceTextView = (TextView)fragmentView.findViewById(R.id.location_distance_label);
|
2014-06-03 23:31:48 +00:00
|
|
|
View bottomView = fragmentView.findViewById(R.id.location_bottom_view);
|
|
|
|
TextView sendButton = (TextView) fragmentView.findViewById(R.id.location_send_button);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (sendButton != null) {
|
|
|
|
sendButton.setText(LocaleController.getString("SendLocation", R.string.SendLocation));
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
mapView = (MapView)fragmentView.findViewById(R.id.map_view);
|
|
|
|
mapView.onCreate(null);
|
|
|
|
try {
|
|
|
|
MapsInitializer.initialize(getParentActivity());
|
|
|
|
googleMap = mapView.getMap();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (googleMap != null) {
|
|
|
|
googleMap.setMyLocationEnabled(true);
|
|
|
|
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
|
|
|
|
googleMap.getUiSettings().setZoomControlsEnabled(false);
|
|
|
|
googleMap.getUiSettings().setCompassEnabled(false);
|
|
|
|
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onMyLocationChange(Location location) {
|
|
|
|
positionMarker(location);
|
|
|
|
}
|
|
|
|
});
|
2014-08-08 10:17:06 +00:00
|
|
|
myLocation = getLastLocation();
|
2014-06-03 23:31:48 +00:00
|
|
|
|
|
|
|
if (sendButton != null) {
|
|
|
|
userLocation = new Location("network");
|
|
|
|
userLocation.setLatitude(20.659322);
|
|
|
|
userLocation.setLongitude(-11.406250);
|
|
|
|
LatLng latLng = new LatLng(20.659322, -11.406250);
|
|
|
|
userMarker = googleMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin)).draggable(true));
|
|
|
|
|
|
|
|
sendButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2014-08-22 14:24:33 +00:00
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSelectLocation(userLocation.getLatitude(), userLocation.getLongitude());
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
finishFragment();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
|
|
|
|
@Override
|
|
|
|
public void onMarkerDragStart(Marker marker) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMarkerDrag(Marker marker) {
|
|
|
|
userLocationMoved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMarkerDragEnd(Marker marker) {
|
|
|
|
LatLng latLng = marker.getPosition();
|
|
|
|
userLocation.setLatitude(latLng.latitude);
|
|
|
|
userLocation.setLongitude(latLng.longitude);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bottomView != null) {
|
|
|
|
bottomView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if (userLocation != null) {
|
|
|
|
LatLng latLng = new LatLng(userLocation.getLatitude(), userLocation.getLongitude());
|
|
|
|
CameraUpdate position = CameraUpdateFactory.newLatLngZoom(latLng, googleMap.getMaxZoomLevel() - 8);
|
|
|
|
googleMap.animateCamera(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messageObject != null) {
|
|
|
|
int fromId = messageObject.messageOwner.from_id;
|
|
|
|
if (messageObject.messageOwner instanceof TLRPC.TL_messageForwarded) {
|
|
|
|
fromId = messageObject.messageOwner.fwd_from_id;
|
|
|
|
}
|
2014-08-22 14:24:33 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(fromId);
|
2014-06-03 23:31:48 +00:00
|
|
|
if (user != null) {
|
|
|
|
TLRPC.FileLocation photo = null;
|
|
|
|
if (user.photo != null) {
|
|
|
|
photo = user.photo.photo_small;
|
|
|
|
}
|
2014-09-24 02:17:27 +00:00
|
|
|
avatarImageView.setImage(photo, "50_50", AndroidUtilities.getUserAvatarForId(user.id));
|
|
|
|
nameTextView.setText(ContactsController.formatName(user.first_name, user.last_name));
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
|
|
|
userLocation = new Location("network");
|
|
|
|
userLocation.setLatitude(messageObject.messageOwner.media.geo.lat);
|
|
|
|
userLocation.setLongitude(messageObject.messageOwner.media.geo._long);
|
|
|
|
LatLng latLng = new LatLng(userLocation.getLatitude(), userLocation.getLongitude());
|
|
|
|
userMarker = googleMap.addMarker(new MarkerOptions().position(latLng).
|
|
|
|
icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin)));
|
|
|
|
CameraUpdate position = CameraUpdateFactory.newLatLngZoom(latLng, googleMap.getMaxZoomLevel() - 8);
|
|
|
|
googleMap.moveCamera(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
positionMarker(myLocation);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
ViewGroup parent = (ViewGroup)fragmentView.getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.removeView(fragmentView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
2014-08-08 10:17:06 +00:00
|
|
|
private Location getLastLocation() {
|
|
|
|
LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
List<String> providers = lm.getProviders(true);
|
|
|
|
Location l = null;
|
|
|
|
for (int i = providers.size() - 1; i >= 0; i--) {
|
|
|
|
l = lm.getLastKnownLocation(providers.get(i));
|
|
|
|
if (l != null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private void updateUserData() {
|
|
|
|
if (messageObject != null && avatarImageView != null) {
|
|
|
|
int fromId = messageObject.messageOwner.from_id;
|
|
|
|
if (messageObject.messageOwner instanceof TLRPC.TL_messageForwarded) {
|
|
|
|
fromId = messageObject.messageOwner.fwd_from_id;
|
|
|
|
}
|
2014-08-22 14:24:33 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(fromId);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user != null) {
|
|
|
|
TLRPC.FileLocation photo = null;
|
|
|
|
if (user.photo != null) {
|
|
|
|
photo = user.photo.photo_small;
|
|
|
|
}
|
2014-09-24 02:17:27 +00:00
|
|
|
avatarImageView.setImage(photo, null, AndroidUtilities.getUserAvatarForId(user.id));
|
|
|
|
nameTextView.setText(ContactsController.formatName(user.first_name, user.last_name));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void positionMarker(Location location) {
|
|
|
|
if (location == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
myLocation = location;
|
|
|
|
if (messageObject != null) {
|
|
|
|
if (userLocation != null && distanceTextView != null) {
|
|
|
|
float distance = location.distanceTo(userLocation);
|
|
|
|
if (distance < 1000) {
|
2014-03-22 22:31:55 +00:00
|
|
|
distanceTextView.setText(String.format("%d %s", (int)(distance), LocaleController.getString("MetersAway", R.string.MetersAway)));
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
distanceTextView.setText(String.format("%.2f %s", distance / 1000.0f, LocaleController.getString("KMetersAway", R.string.KMetersAway)));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!userLocationMoved && googleMap != null) {
|
|
|
|
userLocation = location;
|
|
|
|
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
|
|
|
|
userMarker.setPosition(latLng);
|
|
|
|
if (firstWas) {
|
|
|
|
CameraUpdate position = CameraUpdateFactory.newLatLng(latLng);
|
|
|
|
googleMap.animateCamera(position);
|
|
|
|
} else {
|
|
|
|
firstWas = true;
|
|
|
|
CameraUpdate position = CameraUpdateFactory.newLatLngZoom(latLng, googleMap.getMaxZoomLevel() - 8);
|
|
|
|
googleMap.moveCamera(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 23:05:54 +00:00
|
|
|
public void setMessageObject(MessageObject message) {
|
|
|
|
messageObject = message;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
2014-08-22 14:24:33 +00:00
|
|
|
if (id == NotificationCenter.updateInterfaces) {
|
2014-02-04 18:36:55 +00:00
|
|
|
int mask = (Integer)args[0];
|
|
|
|
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_NAME) != 0) {
|
|
|
|
updateUserData();
|
|
|
|
}
|
2014-08-22 14:24:33 +00:00
|
|
|
} else if (id == NotificationCenter.closeChats) {
|
2013-10-25 15:19:00 +00:00
|
|
|
removeSelfFromStack();
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
if (mapView != null) {
|
2014-06-14 08:36:01 +00:00
|
|
|
try {
|
|
|
|
mapView.onPause();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (mapView != null) {
|
|
|
|
mapView.onResume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLowMemory() {
|
|
|
|
super.onLowMemory();
|
|
|
|
if (mapView != null) {
|
|
|
|
mapView.onLowMemory();
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 14:24:33 +00:00
|
|
|
|
|
|
|
public void setDelegate(LocationActivityDelegate delegate) {
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|