diff --git a/app/build.gradle b/app/build.gradle index c8c4223..6005c9b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,8 +31,8 @@ android { applicationId "com.aykuttasil.sweetloc" minSdkVersion project.minSdk targetSdkVersion project.sdk - versionCode 5 - versionName "1.0.4" + versionCode 6 + versionName "1.0.5" multiDexEnabled true manifestPlaceholders = [onesignal_app_id : "283c0725-f1ae-434a-8ea5-09f61b1246fc", @@ -124,7 +124,7 @@ dependencies { - compile('com.crashlytics.sdk.android:crashlytics:2.6.1@aar') { + compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') { transitive = true; } apt "org.androidannotations:androidannotations:$AAVersion" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 198db01..8e29fea 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,9 @@ package="com.aykuttasil.sweetloc"> + diff --git a/app/src/main/java/com/aykuttasil/sweetloc/activity/LoginFacebookActivity.java b/app/src/main/java/com/aykuttasil/sweetloc/activity/LoginFacebookActivity.java index 3c60f06..3909701 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/activity/LoginFacebookActivity.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/activity/LoginFacebookActivity.java @@ -125,6 +125,7 @@ public void onCancel() { @Override public void onError(FacebookException error) { Logger.e(error, "facebook:onError"); + SuperHelper.CrashlyticsError(error); } }); } @@ -156,6 +157,8 @@ public void onComplete(@NonNull Task task) { // signed in user can be handled in the listener. if (!task.isSuccessful()) { + SuperHelper.CrashlyticsError(task.getException()); + Logger.e(task.getException(), "HATA"); Toast.makeText(LoginFacebookActivity.this, "Authentication failed.", diff --git a/app/src/main/java/com/aykuttasil/sweetloc/activity/MainActivity.java b/app/src/main/java/com/aykuttasil/sweetloc/activity/MainActivity.java index e5f232d..881b69d 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/activity/MainActivity.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/activity/MainActivity.java @@ -105,7 +105,7 @@ public Observable call(String userId) { .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { goFragment(); - }); + }, SuperHelper::CrashlyticsError); } else { goFragment(); } @@ -114,6 +114,7 @@ public Observable call(String userId) { @DebugLog private void goFragment() { + SuperHelper.ReplaceFragmentBeginTransaction( MainActivity.this, UserTrackerListFragment_.builder().build(), @@ -133,12 +134,14 @@ private void goFragment() { @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); + updateUi(); } @DebugLog @OnActivityResult(LOGIN_REQUEST_CODE) public void ActivityResultLogin(int resultCode) { + switch (resultCode) { case RESULT_OK: { updateUi(); @@ -149,6 +152,7 @@ public void ActivityResultLogin(int resultCode) { @Override public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_mainactivity, menu); return super.onCreateOptionsMenu(menu); } diff --git a/app/src/main/java/com/aykuttasil/sweetloc/activity/MapsActivity.java b/app/src/main/java/com/aykuttasil/sweetloc/activity/MapsActivity.java index 9851110..2c42ae2 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/activity/MapsActivity.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/activity/MapsActivity.java @@ -22,6 +22,7 @@ import com.aykuttasil.androidbasichelperlib.UiHelper; import com.aykuttasil.sweetloc.BuildConfig; import com.aykuttasil.sweetloc.R; +import com.aykuttasil.sweetloc.app.AppSweetLoc; import com.aykuttasil.sweetloc.app.Const; import com.aykuttasil.sweetloc.db.DbManager; import com.aykuttasil.sweetloc.helper.SuperHelper; @@ -45,10 +46,8 @@ import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; -import com.google.gson.Gson; import com.onesignal.OneSignal; import com.orhanobut.logger.Logger; -import com.patloew.rxlocation.RxLocation; import com.squareup.picasso.Picasso; import com.squareup.picasso.Target; import com.tbruyelle.rxpermissions.RxPermissions; @@ -73,6 +72,7 @@ import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Function; +import io.reactivex.schedulers.Schedulers; @EActivity(R.layout.activity_maps) public class MapsActivity extends BaseActivity implements OnMapReadyCallback, GoogleMap.InfoWindowAdapter, GoogleMap.OnInfoWindowClickListener { @@ -100,6 +100,7 @@ public class MapsActivity extends BaseActivity implements OnMapReadyCallback, Go @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mCompositeDisposible = new CompositeDisposable(); } @@ -185,10 +186,8 @@ public void onMapReady(GoogleMap googleMap) { @DebugLog private void initLocationListener() { - RxLocation rxLocation = new RxLocation(this); - LocationRequest locationRequest = LocationRequest.create() - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) + .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(30000) .setFastestInterval(5000); @@ -198,7 +197,7 @@ private void initLocationListener() { .build(); - Disposable disposable = rxLocation.settings().checkAndHandleResolution(locationSettingsRequest) + Disposable disposable = ((AppSweetLoc) getApplication()).rxLocation.settings().checkAndHandleResolution(locationSettingsRequest) .flatMapObservable(new Function>() { @DebugLog @Override @@ -210,16 +209,22 @@ public ObservableSource apply(Boolean granted) throws Exception { } if (granted) { - return rxLocation.location().updates(locationRequest); + return ((AppSweetLoc) getApplication()).rxLocation.location().updates(locationRequest); } else { return Observable.error(new Exception("GPS ayarlarında hata var.")); } } }) - .retry() - .subscribe(location -> { - SuperHelper.sendLocationInformation(location); - }); + .retry() // Eğer onError çalışırsa tekrar subscribe olunuyor + .subscribeOn(Schedulers.io()) + .subscribe( + SuperHelper::sendLocationInformation, + error -> { + Logger.e(error, "HATA"); + SuperHelper.CrashlyticsError(error); + } + + ); mCompositeDisposible.add(disposable); @@ -261,9 +266,7 @@ private void setMap() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { - - Logger.i(s); - Logger.i(new Gson().toJson(dataSnapshot.getValue())); + //Logger.i(new Gson().toJson(dataSnapshot.getValue())); ModelLocation modelLocation = dataSnapshot.getValue(ModelLocation.class); @@ -427,6 +430,7 @@ public View getInfoContents(Marker marker) { } catch (Exception e) { + SuperHelper.CrashlyticsError(e); e.printStackTrace(); } return vi; @@ -435,6 +439,7 @@ public View getInfoContents(Marker marker) { @DebugLog @Override public void onInfoWindowClick(Marker marker) { + LatLng markerLatLng = marker.getPosition(); mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(markerLatLng, 15.0f)); } diff --git a/app/src/main/java/com/aykuttasil/sweetloc/app/AppSweetLoc.java b/app/src/main/java/com/aykuttasil/sweetloc/app/AppSweetLoc.java index c75ebd8..1989bc0 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/app/AppSweetLoc.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/app/AppSweetLoc.java @@ -19,6 +19,7 @@ import com.onesignal.OneSignal; import com.orhanobut.logger.LogLevel; import com.orhanobut.logger.Logger; +import com.patloew.rxlocation.RxLocation; import hugo.weaving.DebugLog; import io.fabric.sdk.android.Fabric; @@ -28,6 +29,8 @@ */ public class AppSweetLoc extends Application { + public RxLocation rxLocation; + @Override public void onCreate() { super.onCreate(); @@ -70,9 +73,9 @@ public void initSweetLoc() { .init(); FacebookSdk.sdkInitialize(getApplicationContext()); - AppEventsLogger.activateApp(this); + rxLocation = new RxLocation(this); } diff --git a/app/src/main/java/com/aykuttasil/sweetloc/helper/SuperHelper.java b/app/src/main/java/com/aykuttasil/sweetloc/helper/SuperHelper.java index 66d3dcb..e2518c8 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/helper/SuperHelper.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/helper/SuperHelper.java @@ -12,6 +12,7 @@ import com.aykuttasil.sweetloc.model.ModelLocation; import com.aykuttasil.sweetloc.model.ModelUser; import com.aykuttasil.sweetloc.receiver.SingleLocationRequestReceiver; +import com.crashlytics.android.Crashlytics; import com.facebook.login.LoginManager; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; @@ -111,4 +112,29 @@ public static void stopPeriodicTask(Context context) { alarmManager.cancel(pendingIntent); } + @DebugLog + public static void CrashlyticsError(Throwable error) { + + if (DbManager.getModelUser() != null && DbManager.getModelUser().getEmail() != null) { + Crashlytics.setUserEmail(DbManager.getModelUser().getEmail()); + Crashlytics.logException(error); + } else { + Crashlytics.logException(error); + } + + } + + @DebugLog + public static void CrashlyticsLog(String log) { + + if (DbManager.getModelUser() != null && DbManager.getModelUser().getEmail() != null) { + Crashlytics.setUserEmail(DbManager.getModelUser().getEmail()); + Crashlytics.log(log); + } else { + Crashlytics.log(log); + } + + } + + } diff --git a/app/src/main/java/com/aykuttasil/sweetloc/receiver/SingleLocationRequestReceiver.java b/app/src/main/java/com/aykuttasil/sweetloc/receiver/SingleLocationRequestReceiver.java index 7b6e6a6..4415840 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/receiver/SingleLocationRequestReceiver.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/receiver/SingleLocationRequestReceiver.java @@ -2,9 +2,9 @@ import android.content.Context; import android.content.Intent; -import android.support.v4.content.WakefulBroadcastReceiver; import com.aykuttasil.sweetloc.service.SingleLocationRequestService; +import com.aykuttasil.sweetloc.util.MyWakefulBroadcastReceiver; import hugo.weaving.DebugLog; @@ -12,7 +12,7 @@ * Created by aykutasil on 14.12.2016. */ -public class SingleLocationRequestReceiver extends WakefulBroadcastReceiver { +public class SingleLocationRequestReceiver extends MyWakefulBroadcastReceiver { @DebugLog @Override @@ -20,4 +20,6 @@ public void onReceive(Context context, Intent intent) { Intent intent_ = new Intent(context, SingleLocationRequestService.class); SingleLocationRequestReceiver.startWakefulService(context, intent_); } + + } diff --git a/app/src/main/java/com/aykuttasil/sweetloc/service/SingleLocationRequestService.java b/app/src/main/java/com/aykuttasil/sweetloc/service/SingleLocationRequestService.java index 086124d..61aef53 100644 --- a/app/src/main/java/com/aykuttasil/sweetloc/service/SingleLocationRequestService.java +++ b/app/src/main/java/com/aykuttasil/sweetloc/service/SingleLocationRequestService.java @@ -4,19 +4,18 @@ import android.app.IntentService; import android.content.Intent; import android.content.pm.PackageManager; -import android.location.Location; import android.support.v4.app.ActivityCompat; +import com.aykuttasil.sweetloc.app.AppSweetLoc; import com.aykuttasil.sweetloc.receiver.SingleLocationRequestReceiver; import com.google.android.gms.location.LocationRequest; import com.orhanobut.logger.Logger; -import com.patloew.rxlocation.RxLocation; + +import java.util.concurrent.TimeUnit; import hugo.weaving.DebugLog; import io.reactivex.Observable; -import io.reactivex.ObservableSource; import io.reactivex.disposables.Disposable; -import io.reactivex.functions.Function; import static com.aykuttasil.sweetloc.helper.SuperHelper.sendLocationInformation; @@ -26,6 +25,9 @@ public class SingleLocationRequestService extends IntentService { + + // Konum belirlenip yollanabilmesi için max beklenecek zaman + private static long LOCATION_TIMEOUT_IN_SECONDS = 45; Disposable mDisposable; @DebugLog @@ -37,32 +39,36 @@ public SingleLocationRequestService() { @Override protected void onHandleIntent(Intent intent) { - RxLocation rxLocation = new RxLocation(this); - - LocationRequest locationRequest = LocationRequest.create() - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) - .setInterval(50000) - .setFastestInterval(5000); + if (mDisposable != null && !mDisposable.isDisposed()) { + Logger.i("RxLocation disposed"); + mDisposable.dispose(); + } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Logger.i("Permission is not granted"); return; } - mDisposable = rxLocation.location() + + LocationRequest locationRequest = LocationRequest.create() + .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) + .setInterval(10000) + .setFastestInterval(5000) + //.setNumUpdates(1) // Sadece belirttiğimiz miktar kadar (1) location güncellemesi alır + //.setExpirationTime() // gerçek zaman vererek location güncellemesi kontrolü yapılabilir. (gereksiz) + //.setMaxWaitTime() // her bir location güncellemesi için max bekleme süresini belirtebiliriz. setInterval ile ilişkilidir. dikkat et. + .setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS)); // Belirttiğimiz süre kadar location güncellemesi alır + + mDisposable = ((AppSweetLoc) getApplication()).rxLocation.location() .updates(locationRequest) - .flatMap(new Function>() { - @DebugLog - @Override - public ObservableSource apply(Location location) throws Exception { - if (location.getAccuracy() < 200) { - return Observable.just(location); - } else { - return Observable.error(new Exception("Accuracy > 300")); - } - } + .filter(location -> { + + Logger.i("Accuracy: " + location.getAccuracy()); + + return location.getAccuracy() < 150; + }) - .retry() + .timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS, Observable.error(new Exception("Konum sapması çok yüksek."))) .subscribe(location -> { sendLocationInformation(location); @@ -72,11 +78,13 @@ public ObservableSource apply(Location location) throws Exception { SingleLocationRequestReceiver.completeWakefulIntent(intent); }, error -> { + + mDisposable.dispose(); + Logger.e(error, "HATA"); }); - } - + } @DebugLog @Override diff --git a/app/src/main/java/com/aykuttasil/sweetloc/util/MyWakefulBroadcastReceiver.java b/app/src/main/java/com/aykuttasil/sweetloc/util/MyWakefulBroadcastReceiver.java new file mode 100644 index 0000000..ad37a93 --- /dev/null +++ b/app/src/main/java/com/aykuttasil/sweetloc/util/MyWakefulBroadcastReceiver.java @@ -0,0 +1,105 @@ +package com.aykuttasil.sweetloc.util; + +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.PowerManager; +import android.util.SparseArray; + +import com.orhanobut.logger.Logger; + +/** + * Created by aykutasil on 16.12.2016. + */ + +public abstract class MyWakefulBroadcastReceiver extends BroadcastReceiver { + + private static final String EXTRA_WAKE_LOCK_ID = "android.support.content.wakelockid"; + + private static final SparseArray mActiveWakeLocks + = new SparseArray(); + private static int mNextId = 1; + + /** + * Do a {@link android.content.Context#startService(android.content.Intent) + * Context.startService}, but holding a wake lock while the service starts. + * This will modify the Intent to hold an extra identifying the wake lock; + * when the service receives it in {@link android.app.Service#onStartCommand + * Service.onStartCommand}, it should pass back the Intent it receives there to + * {@link #completeWakefulIntent(android.content.Intent)} in order to release + * the wake lock. + * + * @param context The Context in which it operate. + * @param intent The Intent with which to start the service, as per + * {@link android.content.Context#startService(android.content.Intent) + * Context.startService}. + */ + public static ComponentName startWakefulService(Context context, Intent intent) { + + Logger.i("MyWakefulBroadcastReceiver startWakefulService"); + + synchronized (mActiveWakeLocks) { + int id = mNextId; + mNextId++; + if (mNextId <= 0) { + mNextId = 1; + } + + intent.putExtra(EXTRA_WAKE_LOCK_ID, id); + ComponentName comp = context.startService(intent); + if (comp == null) { + return null; + } + + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + "wake:" + comp.flattenToShortString()); + wl.setReferenceCounted(false); + wl.acquire(60 * 1000); + mActiveWakeLocks.put(id, wl); + + Logger.i("MyWakefulBroadcastReceiver active wake lock id: " + id); + + return comp; + } + } + + /** + * Finish the execution from a previous {@link #startWakefulService}. Any wake lock + * that was being held will now be released. + * + * @param intent The Intent as originally generated by {@link #startWakefulService}. + * @return Returns true if the intent is associated with a wake lock that is + * now released; returns false if there was no wake lock specified for it. + */ + public static boolean completeWakefulIntent(Intent intent) { + + Logger.i("MyWakefulBroadcastReceiver completeWakefulIntent"); + + final int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0); + if (id == 0) { + return false; + } + synchronized (mActiveWakeLocks) { + PowerManager.WakeLock wl = mActiveWakeLocks.get(id); + if (wl != null) { + wl.release(); + mActiveWakeLocks.remove(id); + + Logger.i("MyWakefulBroadcastReceiver remove id: " + id); + + return true; + } + // We return true whether or not we actually found the wake lock + // the return code is defined to indicate whether the Intent contained + // an identifier for a wake lock that it was supposed to match. + // We just log a warning here if there is no wake lock found, which could + // happen for example if this function is called twice on the same + // intent or the process is killed and restarted before processing the intent. + Logger.w("WakefulBroadcastReceiver No active wake lock id #" + id); + return true; + } + } + +}