1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Add notification permission for api level >=33 (fixes #1976) (#1983)

Added notification permission.

---------

Co-authored-by: Ashutosh Soni <ashutoshsoni@bitcot.com>
This commit is contained in:
Ashutosh soni 2023-10-11 22:23:29 +05:30 committed by GitHub
parent c333d858ab
commit 2ff421a166
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 5 deletions

View file

@ -7,6 +7,7 @@
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

View file

@ -14,6 +14,7 @@ import android.Manifest;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@ -29,8 +30,6 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -44,7 +43,6 @@ import com.nutomic.syncthingandroid.util.PermissionUtil;
import com.nutomic.syncthingandroid.util.Util;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
@ -53,10 +51,13 @@ import javax.inject.Inject;
public class FirstStartActivity extends Activity {
private enum Slide {
INTRO(R.layout.activity_firststart_slide_intro),
STORAGE(R.layout.activity_firststart_slide_storage),
LOCATION(R.layout.activity_firststart_slide_location),
API_LEVEL_30(R.layout.activity_firststart_slide_api_level_30);
API_LEVEL_30(R.layout.activity_firststart_slide_api_level_30),
NOTIFICATION(R.layout.activity_firststart_slide_notification);
public final int layout;
@ -191,6 +192,17 @@ public class FirstStartActivity extends Activity {
return mPreferences.getBoolean(Constants.PREF_FIRST_START, true);
}
@TargetApi(33)
private boolean isNotificationPermissionGranted() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return true;
}
return ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
}
private boolean upgradedToApiLevel30() {
if (mPreferences.getBoolean(Constants.PREF_UPGRADED_TO_API_LEVEL_30, false)) {
return true;
@ -234,6 +246,9 @@ public class FirstStartActivity extends Activity {
// Skip if running as root, as that circumvents any Android FS restrictions.
return upgradedToApiLevel30()
|| PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_USE_ROOT, false);
case NOTIFICATION:
return isNotificationPermissionGranted();
}
return false;
}
@ -328,6 +343,15 @@ public class FirstStartActivity extends Activity {
}
});
break;
case NOTIFICATION:
Button notificationBtn = (Button) view.findViewById(R.id.btn_notification);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestNotificationPermission();
}
});
break;
}
container.addView(view);
@ -389,6 +413,13 @@ public class FirstStartActivity extends Activity {
Constants.PermissionRequestType.LOCATION.ordinal());
}
@TargetApi(33)
private void requestNotificationPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
}
}
private void requestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestAllFilesAccessPermission();

View file

@ -0,0 +1,7 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="?attr/colorControlNormal" android:pathData="M7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42zM18,11c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2v-5zM12,22c0.14,0 0.27,-0.01 0.4,-0.04 0.65,-0.14 1.18,-0.58 1.44,-1.18 0.1,-0.24 0.15,-0.5 0.15,-0.78h-4c0.01,1.1 0.9,2 2.01,2z"/>
</vector>

View file

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingBottom="@dimen/dots_full_height">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="@string/enable_notification_permission"
android:textSize="30sp" />
<ImageView
android:layout_width="@dimen/img_width_height"
android:layout_height="@dimen/img_width_height"
android:contentDescription="@null"
android:src="@drawable/baseline_notifications_active_24"
android:tint="?attr/colorSecondary"
tools:ignore="UseAppTint" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_notification"
style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/grant_permission"
android:drawableStart="@android:drawable/ic_menu_save"
android:paddingStart="40dp"
android:paddingLeft="40dp"
android:paddingEnd="40dp"
android:paddingRight="40dp"
android:text="@string/grant_permission"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notification_permission_title"
android:textSize="@dimen/slide_title"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/desc_marginTop"
android:paddingLeft="@dimen/desc_padding"
android:paddingRight="@dimen/desc_padding"
android:paddingBottom="@dimen/desc_paddingBottom"
android:text="@string/require_notification_permission_desc"
android:textAlignment="center"
android:textSize="@dimen/slide_desc" />
</LinearLayout>
</ScrollView>

View file

@ -823,5 +823,8 @@ Please report any problems you encounter via Github.</string>
<string name="browse">Browse</string>
<string name="no_sub_folder_is_selected">No sub folder is selected</string>
<string name="share_device_id_chooser">Share device ID with</string>
<string name="require_notification_permission_desc">Syncthing requires your permission to show you notifications.Those are used to let you know about a new device connecting, or a new folder sharing request.</string>
<string name="notification_permission_title">Notification permission</string>
<string name="enable_notification_permission">Enable Notification Permission</string>
</resources>