- * Utility method as this helper works with it a lot. - * - * @return The system service NotificationManager - */ - private NotificationManager getNotificationManager() { - if (mNotificationManager == null) { - mNotificationManager = (NotificationManager) mContext.getSystemService( - Context.NOTIFICATION_SERVICE); - } - return mNotificationManager; - } - - /** - * Displays a notification with the location results. - */ - void showNotification() { - Intent notificationIntent = new Intent(mContext, MainActivity.class); - - // Construct a task stack. - TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext); - - // Add the main Activity to the task stack as the parent. - stackBuilder.addParentStack(MainActivity.class); - - // Push the content Intent onto the stack. - stackBuilder.addNextIntent(notificationIntent); - - // Get a PendingIntent containing the entire back stack. - PendingIntent notificationPendingIntent = - stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); - - Notification.Builder notificationBuilder = null; - - notificationBuilder = new Notification.Builder(mContext, - PRIMARY_CHANNEL) - .setContentTitle(getLocationResultTitle()) - .setContentText(getLocationResultText()) - .setSmallIcon(R.mipmap.ic_launcher) - .setAutoCancel(true) - .setContentIntent(notificationPendingIntent); - - getNotificationManager().notify(0, notificationBuilder.build()); - } -} diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/LocationService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/LocationService.java deleted file mode 100644 index ecbd540..0000000 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/LocationService.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.uam.wmi.findmytutor.service; - -import android.Manifest; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.IBinder; -import android.support.v4.app.ActivityCompat; -import android.util.Log; -import android.widget.Toast; - -public class LocationService extends Service { - public static final String BROADCAST_ACTION = "Hello World"; - private static final int TWO_MINUTES = 1000 * 60 * 2; - public LocationManager locationManager; - public MyLocationListener listener; - public Location previousBestLocation = null; - - Intent intent; - int counter = 0; - - @Override - public void onCreate() { - super.onCreate(); - intent = new Intent(BROADCAST_ACTION); - - locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - listener = new MyLocationListener(); - if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return; - } - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener); - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); - } - - - @Override - public IBinder onBind(Intent intent) - { - return null; - } - - protected boolean isBetterLocation(Location location, Location currentBestLocation) { - if (currentBestLocation == null) { - // A new location is always better than no location - return true; - } - - // Check whether the new location fix is newer or older - long timeDelta = location.getTime() - currentBestLocation.getTime(); - boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; - boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; - boolean isNewer = timeDelta > 0; - - // If it's been more than two minutes since the current location, use the new location - // because the user has likely moved - if (isSignificantlyNewer) { - return true; - // If the new location is more than two minutes older, it must be worse - } else if (isSignificantlyOlder) { - return false; - } - - // Check whether the new location fix is more or less accurate - int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); - boolean isLessAccurate = accuracyDelta > 0; - boolean isMoreAccurate = accuracyDelta < 0; - boolean isSignificantlyLessAccurate = accuracyDelta > 200; - - // Check if the old and new location are from the same provider - boolean isFromSameProvider = isSameProvider(location.getProvider(), - currentBestLocation.getProvider()); - - // Determine location quality using a combination of timeliness and accuracy - if (isMoreAccurate) { - return true; - } else if (isNewer && !isLessAccurate) { - return true; - } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { - return true; - } - return false; - } - - - - /** Checks whether two providers are the same */ - private boolean isSameProvider(String provider1, String provider2) { - if (provider1 == null) { - return provider2 == null; - } - return provider1.equals(provider2); - } - - - - @Override - public void onDestroy() { - // handler.removeCallbacks(sendUpdatesToUI); - super.onDestroy(); - Log.v("STOP_SERVICE", "DONE"); - locationManager.removeUpdates((LocationListener) listener); - } - - public static Thread performOnBackgroundThread(final Runnable runnable) { - final Thread t = new Thread() { - @Override - public void run() { - try { - runnable.run(); - } finally { - - } - } - }; - t.start(); - return t; - } - - - - - public class MyLocationListener implements LocationListener - { - - public void onLocationChanged(final Location loc) - { - Log.i("*******", "Location changed"); - if(isBetterLocation(loc, previousBestLocation)) { - loc.getLatitude(); - loc.getLongitude(); - - Log.e("service", String.valueOf(loc)); - intent.putExtra("Latitude", loc.getLatitude()); - intent.putExtra("Longitude", loc.getLongitude()); - intent.putExtra("Provider", loc.getProvider()); - sendBroadcast(intent); - - } - } - - - public void onProviderDisabled(String provider) - { - Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); - } - - - public void onProviderEnabled(String provider) - { - Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); - } - - - public void onStatusChanged(String provider, int status, Bundle extras) - { - - } - - } -} \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/MyDividerItemDecoration.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/MyDividerItemDecoration.java deleted file mode 100644 index 96a4d62..0000000 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/MyDividerItemDecoration.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.uam.wmi.findmytutor.utils; - -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.util.TypedValue; -import android.view.View; - -public class MyDividerItemDecoration extends RecyclerView.ItemDecoration { - - private static final int[] ATTRS = new int[]{ - android.R.attr.listDivider - }; - - public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; - public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; - - private Drawable mDivider; - private int mOrientation; - private Context context; - private int margin; - - public MyDividerItemDecoration(Context context, int orientation, int margin) { - this.context = context; - this.margin = margin; - final TypedArray a = context.obtainStyledAttributes(ATTRS); - mDivider = a.getDrawable(0); - a.recycle(); - setOrientation(orientation); - } - - public void setOrientation(int orientation) { - if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { - throw new IllegalArgumentException("invalid orientation"); - } - mOrientation = orientation; - } - - @Override - public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { - if (mOrientation == VERTICAL_LIST) { - drawVertical(c, parent); - } else { - drawHorizontal(c, parent); - } - } - - public void drawVertical(Canvas c, RecyclerView parent) { - final int left = parent.getPaddingLeft(); - final int right = parent.getWidth() - parent.getPaddingRight(); - - final int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { - final View child = parent.getChildAt(i); - final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child - .getLayoutParams(); - final int top = child.getBottom() + params.bottomMargin; - final int bottom = top + mDivider.getIntrinsicHeight(); - mDivider.setBounds(left + dpToPx(margin), top, right - dpToPx(margin), bottom); - mDivider.draw(c); - } - } - - public void drawHorizontal(Canvas c, RecyclerView parent) { - final int top = parent.getPaddingTop(); - final int bottom = parent.getHeight() - parent.getPaddingBottom(); - - final int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { - final View child = parent.getChildAt(i); - final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child - .getLayoutParams(); - final int left = child.getRight() + params.rightMargin; - final int right = left + mDivider.getIntrinsicHeight(); - mDivider.setBounds(left, top + dpToPx(margin), right, bottom - dpToPx(margin)); - mDivider.draw(c); - } - } - - @Override - public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { - if (mOrientation == VERTICAL_LIST) { - outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); - } else { - outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); - } - } - - private int dpToPx(int dp) { - Resources r = context.getResources(); - return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics())); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/RecyclerTouchListener.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/RecyclerTouchListener.java deleted file mode 100644 index 28aef5d..0000000 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/RecyclerTouchListener.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.uam.wmi.findmytutor.utils; - -import android.content.Context; -import android.support.v7.widget.RecyclerView; -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; - -public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { - - private ClickListener clicklistener; - private GestureDetector gestureDetector; - - public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener) { - - this.clicklistener = clicklistener; - gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { - @Override - public boolean onSingleTapUp(MotionEvent e) { - return true; - } - - @Override - public void onLongPress(MotionEvent e) { - View child = recycleView.findChildViewUnder(e.getX(), e.getY()); - if (child != null && clicklistener != null) { - clicklistener.onLongClick(child, recycleView.getChildAdapterPosition(child)); - } - } - }); - } - - @Override - public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { - View child = rv.findChildViewUnder(e.getX(), e.getY()); - if (child != null && clicklistener != null && gestureDetector.onTouchEvent(e)) { - clicklistener.onClick(child, rv.getChildAdapterPosition(child)); - } - return false; - } - - @Override - public void onTouchEvent(RecyclerView rv, MotionEvent e) { - } - - @Override - public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { - } - - public interface ClickListener { - void onClick(View view, int position); - void onLongClick(View view, int position); - } -} \ No newline at end of file