This commit is contained in:
Mieszko Wrzeszczyński 2018-11-03 16:02:01 +01:00
parent 2c61d22db7
commit 8553b934a6
4 changed files with 58 additions and 91 deletions

View File

@ -13,9 +13,9 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-feature android:name="android.hardware.location.gps" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,11 +1,11 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.Manifest;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -19,7 +19,6 @@ import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -31,12 +30,20 @@ import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.ActiveFragment; import com.uam.wmi.findmytutor.utils.ActiveFragment;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class BaseActivity public abstract class BaseActivity
extends AppCompatActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener { implements BottomNavigationView.OnNavigationItemSelectedListener {
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS };
protected static final int REQUEST_PERMISSIONS = 100; protected static final int REQUEST_PERMISSIONS = 100;
public DrawerLayout drawerLayout; public DrawerLayout drawerLayout;
protected BottomNavigationView navigationView; protected BottomNavigationView navigationView;
@ -44,7 +51,7 @@ public abstract class BaseActivity
protected DrawerLayout sideDrawer; protected DrawerLayout sideDrawer;
protected Toolbar toolbar; protected Toolbar toolbar;
protected boolean isTutor; protected boolean isTutor;
boolean boolean_permission;
private ActionBarDrawerToggle actionBarDrawerToggle; private ActionBarDrawerToggle actionBarDrawerToggle;
private SharingFragment sharingFragment; private SharingFragment sharingFragment;
private Fragment userListFragment; private Fragment userListFragment;
@ -104,7 +111,49 @@ public abstract class BaseActivity
if (!isTutor) { if (!isTutor) {
navigationView.findViewById(R.id.nav_profile).setVisibility(View.GONE); navigationView.findViewById(R.id.nav_profile).setVisibility(View.GONE);
} }
checkPermissions();
}
protected void checkPermissions() {
final List<String> missingPermissions = new ArrayList<String>();
for (final String permission : REQUIRED_SDK_PERMISSIONS) {
final int result = ContextCompat.checkSelfPermission(this, permission);
if (result != PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(permission);
}
}
if (!missingPermissions.isEmpty()) {
// request all missing permissions
final String[] permissions = missingPermissions
.toArray(new String[missingPermissions.size()]);
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS);
} else {
final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length];
Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED);
onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS,
grantResults);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
for (int index = permissions.length - 1; index >= 0; --index) {
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
// exit the app if one permission is not granted
Toast.makeText(this, "Required permission '" + permissions[index]
+ "' not granted, exiting", Toast.LENGTH_LONG).show();
finish();
return;
}
}
break;
}
} }
public void stopBackgroundLocalizationTask() { public void stopBackgroundLocalizationTask() {
@ -182,39 +231,8 @@ public abstract class BaseActivity
actionBarDrawerToggle.syncState(); actionBarDrawerToggle.syncState();
/* if (isTutor) {
startLocalizationService();
}*/
} }
/* public void startLocalizationService() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(BaseActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(BaseActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}*/
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
@ -264,20 +282,6 @@ public abstract class BaseActivity
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override @Override
protected void onStart() { protected void onStart() {

View File

@ -198,22 +198,6 @@ public class MapActivity extends BaseActivity
} }
public void setLocale(String localeName) {
if (!localeName.equals(currentLanguage)) {
myLocale = new Locale(localeName);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MapActivity.class);
refresh.putExtra(currentLang, localeName);
startActivity(refresh);
} else {
Toast.makeText(this, "Language already selected!", Toast.LENGTH_SHORT).show();
}
}
// Add the mapView lifecycle to the activity's lifecycle methods // Add the mapView lifecycle to the activity's lifecycle methods

View File

@ -1,14 +1,9 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
@ -16,39 +11,28 @@ import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast;
import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; import java.util.Locale;
public class SettingsActivity extends AppCompatPreferenceActivity { public class SettingsActivity extends AppCompatPreferenceActivity {
private static final String TAG = SettingsActivity.class.getSimpleName(); private static final String TAG = SettingsActivity.class.getSimpleName();
Locale myLocale;
String currentLanguage = "pl", currentLang; String currentLang;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit(); getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
} }
public void setLocale(String localeName) { public void setLocale(String localeName) {
Locale myLocale = new Locale(localeName); Locale myLocale = new Locale(localeName);
Resources res = getResources(); Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics(); DisplayMetrics dm = res.getDisplayMetrics();
@ -60,7 +44,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
startActivity(refresh); startActivity(refresh);
} }
public static class MainPreferenceFragment extends PreferenceFragment { public static class MainPreferenceFragment extends PreferenceFragment {
@SuppressLint("ResourceType") @SuppressLint("ResourceType")
@Override @Override
@ -71,6 +54,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
Preference languagesList = findPreference(getResources().getString(R.string.key_language)); Preference languagesList = findPreference(getResources().getString(R.string.key_language));
languagesList.setOnPreferenceChangeListener((preference, newValue) -> { languagesList.setOnPreferenceChangeListener((preference, newValue) -> {
if (!newValue.toString().equals("0")){ if (!newValue.toString().equals("0")){
((SettingsActivity)getActivity()).setLocale("pl"); ((SettingsActivity)getActivity()).setLocale("pl");
PrefUtils.storeLocale(getActivity(),"pl"); PrefUtils.storeLocale(getActivity(),"pl");
@ -81,16 +65,11 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
return true; return true;
}); });
} }
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) { if (item.getItemId() == android.R.id.home) {