Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -29,8 +30,6 @@
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;
Expand All @@ -44,7 +43,6 @@
import com.nutomic.syncthingandroid.util.Util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

Expand All @@ -53,7 +51,10 @@
public class FirstStartActivity extends Activity {

private enum Slide {

INTRO(R.layout.activity_firststart_slide_intro),

NOTIFICATION(R.layout.actiivty_notification),
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);
Expand All @@ -63,7 +64,9 @@ private enum Slide {
Slide(int layout) {
this.layout = layout;
}
};
}

;
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved

private static Slide[] slides = Slide.values();
private static String TAG = "FirstStartActivity";
Expand Down Expand Up @@ -191,6 +194,16 @@ private boolean isFirstStart() {
return mPreferences.getBoolean(Constants.PREF_FIRST_START, true);
}

@TargetApi(33)
private boolean isNotificationPermissionGranted() {
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return true;
} else {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
}
bt90 marked this conversation as resolved.
Show resolved Hide resolved
}


private boolean upgradedToApiLevel30() {
if (mPreferences.getBoolean(Constants.PREF_UPGRADED_TO_API_LEVEL_30, false)) {
return true;
Expand Down Expand Up @@ -226,6 +239,9 @@ private boolean shouldSkipSlide(Slide slide) {
switch (slide) {
case INTRO:
return !isFirstStart();
case NOTIFICATION:
return isNotificationPermissionGranted();

case STORAGE:
return PermissionUtil.haveStoragePermission(this);
case LOCATION:
Expand Down Expand Up @@ -295,6 +311,16 @@ public Object instantiateItem(ViewGroup container, int position) {
View view = layoutInflater.inflate(slides[position].layout, container, false);

switch (slides[position]) {
case NOTIFICATION:
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved
Button notificationBtn = (Button) view.findViewById(R.id.btn_notification);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestNotificationPermission();
}
});
break;

case INTRO:
break;

Expand Down Expand Up @@ -364,7 +390,7 @@ private void startApp() {
* so that back navigation works as expected.
*/
if (mPreferences.getBoolean(Constants.PREF_START_INTO_WEB_GUI, false)) {
startActivities(new Intent[] {mainIntent, new Intent(this, WebGuiActivity.class)});
startActivities(new Intent[]{mainIntent, new Intent(this, WebGuiActivity.class)});
} else {
startActivity(mainIntent);
}
Expand All @@ -389,6 +415,19 @@ private void requestLocationPermission() {
Constants.PermissionRequestType.LOCATION.ordinal());
}

@TargetApi(33)
private void requestNotificationPermission() {
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved
bt90 marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<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="@android:color/white" 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"/>
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved
</vector>
68 changes: 68 additions & 0 deletions app/src/main/res/layout/actiivty_notification.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
bt90 marked this conversation as resolved.
Show resolved Hide resolved
<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>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.</string>
githubashutoshsoni marked this conversation as resolved.
Show resolved Hide resolved
<string name="notification_permission_title">Notification permission</string>
<string name="enable_notification_permission">Enable Notification Permission</string>

</resources>