Skip to content

Commit

Permalink
Update to Android 12
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementGre committed May 1, 2022
1 parent e009290 commit bf936f6
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ Le fichier .apk de l'application peut être téléchargé dans l'[onglet Release

## Preview

![](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/app/src/main/res/preview/preview1.png) ![](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/app/src/main/res/preview/preview2.png)
![](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/preview/preview1.png) ![](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/preview/preview2.png)

![Logo](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/app/src/main/res/preview/logo_semi_rounded.png)
![Logo](https://raw.githubusercontent.com/ClementGre/Monitor-Internetless/master/preview/logo_semi_rounded.png)
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion '30.0.2'

defaultConfig {
applicationId "fr.themsou.monitorinternetless"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package fr.themsou.monitorinternetless;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.util.Consumer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

Expand Down Expand Up @@ -54,21 +55,8 @@ public static boolean isGranted(Context context, String... permissionsCode){
return grant;
}

public void grant(String permissionCode){
if(!isGranted(permissionCode))
ActivityCompat.requestPermissions(activity, new String[]{permissionCode}, 2000);
}

public void grant(String permissionCode, Consumer<Boolean> callBack){
if(!isGranted(permissionCode)){
int requestCode = 2000 + new Random().nextInt(1000); // 2000 - 2999
ActivityCompat.requestPermissions(activity, new String[]{permissionCode}, requestCode);
currentRequests.put(requestCode, callBack);
}else{
callBack.accept(true);
}
}
public void grantOnly(String permissionCode, Consumer<Boolean> grantedCallBack){
public void grantOnly(String permissionCode, Consumer<Boolean> grantedCallBack){ // If already granted, return null
if(!isGranted(permissionCode)){
int requestCode = 3000 + new Random().nextInt(1000); // 3000 - 3999
ActivityCompat.requestPermissions(activity, new String[]{permissionCode}, requestCode);
Expand All @@ -78,26 +66,48 @@ public void grantOnly(String permissionCode, Consumer<Boolean> grantedCallBack){
}
}

public void grantSome(String... permissions){
if(!isGranted(permissions))
ActivityCompat.requestPermissions(activity, permissions, 2000);
}
public void grantSome(String[] permissions, Consumer<Boolean> callBack){
if(!isGranted(permissions)){

ArrayList<String> finalPermissions = new ArrayList<>();
for(String permission : permissions){
if(permission.equals(Manifest.permission.ACCESS_BACKGROUND_LOCATION) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
callBack = insertRequestForBackgroundLocation(callBack);
}else{
finalPermissions.add(permission);
}
}
permissions = finalPermissions.toArray(new String[0]);

if(!isGranted(permissions) && permissions.length > 0){
int requestCode = 2000 + new Random().nextInt(1000); // 2000 - 2999
ActivityCompat.requestPermissions(activity, permissions, requestCode);
currentRequests.put(requestCode, callBack);
}else{
callBack.accept(true);
}

}
public void grantSomeOnly(String[] permissions, Consumer<Boolean> grantedCallBack){
if(!isGranted(permissions)){
int requestCode = 3000 + new Random().nextInt(1000); // 3000 - 3999
ActivityCompat.requestPermissions(activity, permissions, requestCode);
currentRequests.put(requestCode, grantedCallBack);
}else{
grantedCallBack.accept(null);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
public Consumer<Boolean> insertRequestForBackgroundLocation(final Consumer<Boolean> callBack){

return new Consumer<Boolean>() {
@Override
public void accept(Boolean isGranted) {
if(isGranted){
if(!isGranted(Manifest.permission.ACCESS_BACKGROUND_LOCATION)){ // Everything's granted but not the background location.
int requestCode = 2000 + new Random().nextInt(1000); // 2000 - 2999
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, requestCode);
currentRequests.put(requestCode, callBack);
}else{
callBack.accept(true);
}
}else{
callBack.accept(false);
}
}
};


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.util.Consumer;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import java.util.ArrayList;

import fr.themsou.monitorinternetless.MainActivity;
import fr.themsou.monitorinternetless.R;
import fr.themsou.monitorinternetless.ui.numbers.Number;
import fr.themsou.monitorinternetless.ui.numbers.NumbersListAdapter;

public class CommandsFragment extends Fragment {

Expand Down Expand Up @@ -55,14 +44,14 @@ public static ArrayList<Command> getCommands(Context context){

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
commands.add(new Command("info", R.drawable.ic_baseline_info_24, R.string.command_title_info, R.string.command_desc_info,
context, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION));
context, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION));
commands.add(new Command("locate", R.drawable.ic_baseline_gps_fixed_24, R.string.command_title_locate, R.string.command_desc_locate,
context, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION));
context, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION));
}else{
commands.add(new Command("info", R.drawable.ic_baseline_info_24, R.string.command_title_info, R.string.command_desc_info,
context, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_COARSE_LOCATION));
context, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION));
commands.add(new Command("locate", R.drawable.ic_baseline_gps_fixed_24, R.string.command_title_locate, R.string.command_desc_locate,
context, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION));
context, Manifest.permission.ACCESS_FINE_LOCATION));
}

//commands.add(new Command("eco", R.drawable.ic_baseline_battery_charging_full_24, R.string.command_title_eco, R.string.command_desc_eco,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

import android.app.Activity;
import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.util.Consumer;
import androidx.room.ColumnInfo;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Entity;
import androidx.room.Insert;
import androidx.room.PrimaryKey;
import androidx.room.Query;
import androidx.room.Room;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Predicate;

@Entity
public class Number{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package fr.themsou.monitorinternetless.ui.numbers;

import static android.app.Activity.RESULT_OK;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds;
import android.text.InputType;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -24,11 +25,10 @@
import androidx.fragment.app.Fragment;

import java.util.ArrayList;

import fr.themsou.monitorinternetless.MainActivity;
import fr.themsou.monitorinternetless.R;

import static android.app.Activity.RESULT_OK;

public class NumbersFragment extends Fragment {

private static final int CONTACT_PICKER_RESULT = 1001;
Expand All @@ -38,7 +38,7 @@ public class NumbersFragment extends Fragment {
private String TAG = "NumbersFragment";

public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState){
((MainActivity) getActivity()).getTopToolBar().setTitle(getString(R.string.title_numbers_full));
((MainActivity) requireActivity()).getTopToolBar().setTitle(getString(R.string.title_numbers_full));

View root = inflater.inflate(R.layout.fragment_numbers, container, false);
final LayoutInflater layoutInflater = LayoutInflater.from(getContext());
Expand All @@ -53,9 +53,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup conta
}
});

root.findViewById(R.id.action_button_contact).setOnClickListener(new View.OnClickListener() {
root.findViewById(R.id.action_add_number_contacts).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
((MainActivity) getActivity()).permissionRequester.grantOnly(Manifest.permission.READ_CONTACTS, new Consumer<Boolean>() {
((MainActivity) requireActivity()).permissionRequester.grantOnly(Manifest.permission.READ_CONTACTS, new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
Expand All @@ -65,7 +65,7 @@ public void accept(Boolean aBoolean) {
}
});

root.findViewById(R.id.action_button_capture).setOnClickListener(new View.OnClickListener() {
root.findViewById(R.id.action_add_number_manually).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(final View v) {
final View inputs = layoutInflater.inflate(R.layout.inputs_numbers, null);
new AlertDialog.Builder(v.getContext())
Expand All @@ -87,22 +87,20 @@ public void accept(Boolean aBoolean) {
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch(requestCode){
case CONTACT_PICKER_RESULT:
assert data != null;
Uri result = data.getData();
if(result != null){
String[] contactInfo = getContactInfo(result.getLastPathSegment());
if(contactInfo != null){
adapter.addItem(new Number(contactInfo[0], contactInfo[1], getActivity()));
}else{
Toast.makeText(getContext(), R.string.number_do_not_have_number, Toast.LENGTH_LONG).show();
}
if(requestCode == CONTACT_PICKER_RESULT){
assert data != null;
Uri result = data.getData();
if(result != null){
String[] contactInfo = getContactInfo(result.getLastPathSegment());
if(contactInfo != null){
adapter.addItem(new Number(contactInfo[0], contactInfo[1], getActivity()));
}else{
Toast.makeText(getContext(), R.string.number_do_not_have_number, Toast.LENGTH_LONG).show();
}
break;
}
}
if(requestCode >= 2000 && requestCode < 3000){
Log.w(TAG, "Warning: receive false requestCode");
Log.w(TAG, "Warning: received false requestCode");
}
}else{
Log.w(TAG, "Warning: activity result not ok");
Expand All @@ -114,7 +112,7 @@ public String getContactName(String number){
}
public String[] getContactInfo(String uriId){

Cursor cursor = getActivity().getContentResolver().query(
Cursor cursor = requireActivity().getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI, new String[]{CommonDataKinds.Phone.NUMBER, CommonDataKinds.Identity.DISPLAY_NAME},
CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{uriId},null);

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_numbers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
app:menu_showShadow="true"
app:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/action_button_contact"
android:id="@+id/action_add_number_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
Expand All @@ -31,7 +31,7 @@
app:fab_label="@string/addnumber_contact"/>

<com.github.clans.fab.FloatingActionButton
android:id="@+id/action_button_capture"
android:id="@+id/action_add_number_manually"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0-alpha14'
classpath 'com.android.tools.build:gradle:7.1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit bf936f6

Please sign in to comment.