added permissions checks and handling
This commit is contained in:
parent
e72bbb3fcf
commit
92138f26bb
7 changed files with 178 additions and 67 deletions
|
@ -91,10 +91,10 @@ public class GPSUtils extends Service implements LocationListener {
|
|||
if (isNetworkEnabled) {
|
||||
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
Log.e("TAG", "No Permissions");
|
||||
((MainActivity)mContext).setLocationPermission(false);
|
||||
Utils.setLocationPermission(false);
|
||||
return null;
|
||||
} else {
|
||||
((MainActivity)mContext).setLocationPermission(true);
|
||||
Utils.setLocationPermission(true);
|
||||
}
|
||||
Log.d(TAG, "Network");
|
||||
if (locationManager != null) {
|
||||
|
@ -120,8 +120,7 @@ public class GPSUtils extends Service implements LocationListener {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
|
||||
}
|
||||
}
|
||||
|
@ -212,7 +211,7 @@ public class GPSUtils extends Service implements LocationListener {
|
|||
|
||||
/**
|
||||
* Function to get latitude
|
||||
* */
|
||||
*/
|
||||
public double getLatitude() {
|
||||
if (location != null) {
|
||||
latitude = location.getLatitude();
|
||||
|
@ -226,7 +225,7 @@ public class GPSUtils extends Service implements LocationListener {
|
|||
|
||||
/**
|
||||
* Function to get longitude
|
||||
* */
|
||||
*/
|
||||
public double getLongitude() {
|
||||
if (location != null) {
|
||||
longitude = location.getLongitude();
|
||||
|
@ -246,7 +245,6 @@ public class GPSUtils extends Service implements LocationListener {
|
|||
String address = "";
|
||||
|
||||
|
||||
|
||||
List<Address> listAddress = getFromLocation(getLatitude(), getLongitude(), 1);
|
||||
for (int i = 0; i <= listAddress.get(0).getMaxAddressLineIndex(); i++) {
|
||||
listAddress.get(0).getAddressLine(i);
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.android_app.matan.ara.sagi.thesocialnotework;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
|
@ -40,6 +42,8 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
|||
private final String LOGIN_PATH = "/login";
|
||||
private LoginActivity self; //this
|
||||
protected LinearLayout layout;
|
||||
private boolean permissionsReturend = false;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -59,6 +63,9 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
|||
this.mLoginButton.setOnClickListener(this);
|
||||
this.mLoginButton.setEnabled(false);
|
||||
|
||||
//check for permission
|
||||
// while(!permissionsReturend);
|
||||
|
||||
}
|
||||
|
||||
private void removeFocuse() {
|
||||
|
@ -220,5 +227,42 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
|||
public void afterTextChanged(Editable editable) {
|
||||
|
||||
}
|
||||
// @Override
|
||||
// public void onRequestPermissionsResult(int requestCode,
|
||||
// String permissions[], int[] grantResults) {
|
||||
// permissionsReturend = true;
|
||||
// Log.d(TAG, "onRequestPermissionsResult: in func");
|
||||
// switch (requestCode) {
|
||||
// case FINE_PERM: {
|
||||
// // If request is cancelled, the result arrays are empty.
|
||||
// if (grantResults.length > 0
|
||||
// && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// Log.d(TAG, "onRequestPermissionsResult: got permission for location");
|
||||
// Utils.setLocationPermission(true);
|
||||
// ActivityCompat.requestPermissions(LoginActivity.this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERM);
|
||||
// } else {
|
||||
// Log.d(TAG, "onRequestPermissionsResult:DIDNT get permission for location");
|
||||
//
|
||||
// Toast.makeText(LoginActivity.this,"No Location Permissions granted.\n\"An App is nothing without its permissions\"",Toast.LENGTH_LONG);
|
||||
//// System.exit(0);
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
// case CAMERA_PERM: {
|
||||
// if (grantResults.length > 0
|
||||
// && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// Log.d(TAG, "onRequestPermissionsResult: got permission for camera");
|
||||
// Utils.setCameraPermission(true);
|
||||
//
|
||||
//
|
||||
// } else {
|
||||
// Log.d(TAG, "onRequestPermissionsResult: DIDNT get permission for camera");
|
||||
// Toast.makeText(LoginActivity.this,"No Camera Permissions granted.\n\"An App is nothing without its permissions\"",Toast.LENGTH_LONG);
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.android_app.matan.ara.sagi.thesocialnotework;
|
||||
|
||||
import android.*;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
|
@ -11,6 +13,7 @@ import android.os.Bundle;
|
|||
import android.os.StrictMode;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -24,6 +27,7 @@ import android.support.v7.widget.Toolbar;
|
|||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
|
@ -44,7 +48,6 @@ public class MainActivity extends AppCompatActivity
|
|||
protected final String TAG = "[TSN / MainActivity]";
|
||||
protected User user;
|
||||
private GPSUtils gpsUtils;
|
||||
private boolean locationPermission;
|
||||
private GmapFragment gmapFragment;
|
||||
private PersonalFragment personalFragment;
|
||||
private SettingsFragment settingsFragment;
|
||||
|
@ -53,6 +56,7 @@ public class MainActivity extends AppCompatActivity
|
|||
private ImageView menu_avatar;
|
||||
private MainActivity self;
|
||||
private NavigationView nav_view;
|
||||
private final int FINE_PERM = 0, CAMERA_PERM = 1;
|
||||
|
||||
|
||||
@Override
|
||||
|
@ -60,6 +64,8 @@ public class MainActivity extends AppCompatActivity
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
this.self = this;
|
||||
|
||||
|
||||
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
toolbar.setTitle("Personal Notes");
|
||||
setSupportActionBar(toolbar);
|
||||
|
@ -69,11 +75,6 @@ public class MainActivity extends AppCompatActivity
|
|||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||
StrictMode.setThreadPolicy(policy);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
|
||||
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
|
||||
|
@ -189,20 +190,44 @@ public class MainActivity extends AppCompatActivity
|
|||
return this.gpsUtils;
|
||||
}
|
||||
|
||||
public void setLocationPermission(boolean locationPermission) {
|
||||
this.locationPermission = locationPermission;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public String getUserId(){return user.getId();}
|
||||
public String getUserId() {
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode,
|
||||
String permissions[], int[] grantResults) {
|
||||
Log.d(TAG, "onRequestPermissionsResult: in func");
|
||||
switch (requestCode) {
|
||||
case FINE_PERM: {
|
||||
// If request is cancelled, the result arrays are empty.
|
||||
if (grantResults.length > 1) {
|
||||
if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)){
|
||||
Log.d(TAG, "onRequestPermissionsResult: Did Not get permission for location");
|
||||
Toast.makeText(MainActivity.this, "No Location Permissions granted.\n\"An App is nothing without its permissions\"", Toast.LENGTH_LONG).show();
|
||||
System.exit(0);
|
||||
|
||||
}
|
||||
|
||||
if (!(grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
|
||||
Log.d(TAG, "onRequestPermissionsResult:DIDNT get permission for camera");
|
||||
Toast.makeText(MainActivity.this, "No Camera Permissions granted.\nyou will not be able to change avatar", Toast.LENGTH_LONG).show();
|
||||
Utils.setCameraPermission(false);
|
||||
} else {
|
||||
Utils.setCameraPermission(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -51,7 +51,6 @@ import java.util.List;
|
|||
public class PersonalFragment extends Fragment {
|
||||
|
||||
protected ListView noteList;
|
||||
private final int FINE_PERM = 0;
|
||||
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
|
||||
private GPSUtils gpsUtils;
|
||||
private List<Note> listOfNotes;
|
||||
|
@ -59,6 +58,8 @@ public class PersonalFragment extends Fragment {
|
|||
private String userId;
|
||||
private final String TAG = "[TSN/PersonalFragment]";
|
||||
private MainActivity activity;
|
||||
private final int FINE_PERM = 0, CAMERA_PERM = 1;
|
||||
|
||||
|
||||
public PersonalFragment() {
|
||||
// Required empty public constructor
|
||||
|
@ -74,8 +75,18 @@ public class PersonalFragment extends Fragment {
|
|||
Bundle bundle = getArguments();
|
||||
this.userId = activity.getUserId();
|
||||
Log.d(TAG, "onCreateView: userID: " + userId);
|
||||
//check for permission
|
||||
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM);
|
||||
|
||||
ActivityCompat.requestPermissions(activity, new String[]{
|
||||
android.Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
android.Manifest.permission.CAMERA
|
||||
},
|
||||
FINE_PERM
|
||||
);
|
||||
// ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM);
|
||||
|
||||
// if (!Utils.arePermissionsGranted()) {
|
||||
// Toast.makeText(getContext(), "Missing some Permissions...\nPlease go to app info and enable them", Toast.LENGTH_LONG).show();
|
||||
// }
|
||||
|
||||
|
||||
this.noteList = (ListView) view.findViewById(R.id.ps_list_listview);
|
||||
|
@ -287,9 +298,6 @@ public class PersonalFragment extends Fragment {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void addNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException {
|
||||
Note addNote = new Note(
|
||||
noteObject.getString("id"),
|
||||
|
@ -394,4 +402,7 @@ public class PersonalFragment extends Fragment {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package com.android_app.matan.ara.sagi.thesocialnotework;
|
||||
|
||||
import android.*;
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -14,6 +18,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class SettingsFragment extends Fragment {
|
||||
private static final String ARG_PARAM1 = "param1";
|
||||
|
@ -105,10 +110,17 @@ public class SettingsFragment extends Fragment {
|
|||
|
||||
public void onClick(View v)
|
||||
{
|
||||
|
||||
if (ActivityCompat.checkSelfPermission((MainActivity) getActivity(),
|
||||
Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
Log.d(TAG, "in camera Button");
|
||||
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
|
||||
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
|
||||
}
|
||||
else{
|
||||
Toast.makeText(getActivity(), "No Location Permissions granted.\n\"An App is nothing without its permissions\"", Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,8 @@ public class Utils {
|
|||
public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
|
||||
public static ProgressDialog progress;
|
||||
private static HashMap<String, Bitmap> bitmapHash = new HashMap<>();
|
||||
|
||||
private static boolean mLocationPermission = false;
|
||||
private static boolean mCameraPermission = false;
|
||||
|
||||
|
||||
public static Bitmap getBitmapFromURL(String url) {
|
||||
|
@ -156,4 +157,24 @@ public class Utils {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public static void setLocationPermission(boolean locationPermission) {
|
||||
mLocationPermission = locationPermission;
|
||||
}
|
||||
public static void setCameraPermission(boolean cameraPermission) {
|
||||
mCameraPermission = cameraPermission;
|
||||
}
|
||||
public static boolean arePermissionsGranted() {
|
||||
return (mLocationPermission && mCameraPermission);
|
||||
}
|
||||
|
||||
public static boolean isCameraPermissionGranted(){
|
||||
return mCameraPermission;
|
||||
}
|
||||
public static boolean isLocationPermissionGranted(){
|
||||
return mLocationPermission;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue