diff options
author | Arne Schwabe <arne@rfc2549.org> | 2022-08-21 20:08:44 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2022-08-21 22:59:08 +0200 |
commit | 017b3f39c2368a41012c0c6a1f000858b305b1d3 (patch) | |
tree | ec8a8c60b9d71a3cfe6c80c734156e64763aa856 /main/src/ui | |
parent | e544282169db8b8e90867bbb087133621bde35ca (diff) |
Implement querying for notification permission (API 33)
Diffstat (limited to 'main/src/ui')
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java | 22 | ||||
-rw-r--r-- | main/src/ui/res/drawable/notifications.xml | 10 | ||||
-rw-r--r-- | main/src/ui/res/layout/vpn_profile_list.xml | 97 |
3 files changed, 86 insertions, 43 deletions
diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java b/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java index c7ee5df1..e833e81f 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/VPNProfileList.java @@ -5,12 +5,14 @@ package de.blinkt.openvpn.fragments; +import android.Manifest; import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; import android.graphics.drawable.Drawable; @@ -20,6 +22,7 @@ import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.fragment.app.ListFragment; @@ -76,6 +79,7 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn private static final int SELECT_PROFILE = 43; private static final int IMPORT_PROFILE = 231; private static final int FILE_PICKER_RESULT_KITKAT = 392; + private static final int RESULT_PERMISSION = 395; private static final int MENU_IMPORT_PROFILE = Menu.FIRST + 1; private static final int MENU_CHANGE_SORTING = Menu.FIRST + 2; private static final int MENU_IMPORT_AS = Menu.FIRST + 3; @@ -85,6 +89,7 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn private ArrayAdapter<VpnProfile> mArrayadapter; private Intent mLastIntent; private VpnProfile defaultVPN; + private View mPermissionView; @Override public void updateState(String state, String logmessage, final int localizedResId, ConnectionStatus level, Intent intent) { @@ -278,10 +283,24 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn if (fab_import != null) fab_import.setOnClickListener(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) + checkForNotificationPermission(v); + + return v; } + private void checkForNotificationPermission(View v) { + mPermissionView = v.findViewById(R.id.notification_permission); + boolean permissionGranted = (requireActivity().checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED); + mPermissionView.setVisibility(permissionGranted ? View.GONE : View.VISIBLE); + + mPermissionView.setOnClickListener((view) -> { + requireActivity().requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, RESULT_PERMISSION); + }); + } + private void setListAdapter() { if (mArrayadapter == null) { mArrayadapter = new VPNArrayAdapter(getActivity(), R.layout.vpn_list_item, R.id.vpn_item_title); @@ -483,9 +502,12 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn VpnProfile profile = ProfileManager.get(getActivity(), profileUUID); if (profile != null) onAddOrDuplicateProfile(profile); + } else if (resultCode == RESULT_PERMISSION) { + checkForNotificationPermission(requireView()); } + if (resultCode != Activity.RESULT_OK) return; diff --git a/main/src/ui/res/drawable/notifications.xml b/main/src/ui/res/drawable/notifications.xml new file mode 100644 index 00000000..b9d27eb7 --- /dev/null +++ b/main/src/ui/res/drawable/notifications.xml @@ -0,0 +1,10 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="48dp" + android:height="48dp" + android:viewportWidth="48" + android:viewportHeight="48" + android:tint="?attr/colorControlNormal"> + <path + android:fillColor="@android:color/white" + android:pathData="M8,38V35H12.2V19.7Q12.2,15.5 14.675,12.225Q17.15,8.95 21.2,8.1V6.65Q21.2,5.5 22.025,4.75Q22.85,4 24,4Q25.15,4 25.975,4.75Q26.8,5.5 26.8,6.65V8.1Q30.85,8.95 33.35,12.225Q35.85,15.5 35.85,19.7V35H40V38ZM24,23.25Q24,23.25 24,23.25Q24,23.25 24,23.25Q24,23.25 24,23.25Q24,23.25 24,23.25ZM24,44Q22.4,44 21.2,42.825Q20,41.65 20,40H28Q28,41.65 26.825,42.825Q25.65,44 24,44ZM15.2,35H32.85V19.7Q32.85,16 30.3,13.4Q27.75,10.8 24.05,10.8Q20.35,10.8 17.775,13.4Q15.2,16 15.2,19.7Z"/> +</vector> diff --git a/main/src/ui/res/layout/vpn_profile_list.xml b/main/src/ui/res/layout/vpn_profile_list.xml index f85222b6..7dd2eb36 100644 --- a/main/src/ui/res/layout/vpn_profile_list.xml +++ b/main/src/ui/res/layout/vpn_profile_list.xml @@ -3,77 +3,88 @@ ~ Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt --> <FrameLayout 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"> + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent"> <LinearLayout - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="vertical" - android:paddingLeft="@dimen/stdpadding" - android:paddingRight="@dimen/stdpadding"> + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:paddingLeft="@dimen/stdpadding" + android:paddingRight="@dimen/stdpadding"> + + <TextView + android:id="@+id/notification_permission" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="@dimen/stdpadding" + android:drawableStart="@drawable/notifications" + android:drawablePadding="@dimen/stdpadding" + tools:visibility="visible" + android:visibility="gone" + android:text="Notification permission missing. This is used to display the status of the VPN and go notify about required user interaction like multi factor authorisation.\n\nClick this message to give the app notification permissions" /> <ListView - android:id="@android:id/list" - android:layout_width="fill_parent" - android:layout_height="fill_parent" - android:descendantFocusability="afterDescendants" /> + android:id="@android:id/list" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:descendantFocusability="afterDescendants" /> <LinearLayout - android:id="@android:id/empty" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:gravity="center" - android:orientation="vertical"> + android:id="@android:id/empty" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:gravity="center" + android:orientation="vertical"> <TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:text="@string/no_vpn_profiles_defined" - android:textAppearance="?android:attr/textAppearanceLarge" /> + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="@string/no_vpn_profiles_defined" + android:textAppearance="?android:attr/textAppearanceLarge" /> <Space - android:layout_width="match_parent" - android:layout_height="12sp" /> + android:layout_width="match_parent" + android:layout_height="12sp" /> <TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:autoLink="web" - android:text="@string/openvpn_is_no_free_vpn" /> + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:autoLink="web" + android:text="@string/openvpn_is_no_free_vpn" /> <Space - android:layout_width="match_parent" - android:layout_height="12sp" /> + android:layout_width="match_parent" + android:layout_height="12sp" /> <TextView - android:id="@+id/add_new_vpn_hint" - android:layout_width="fill_parent" - android:layout_height="wrap_content" - tools:ignore="SelectableText" /> + android:id="@+id/add_new_vpn_hint" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + tools:ignore="SelectableText" /> <TextView - android:id="@+id/import_vpn_hint" - android:layout_width="fill_parent" - android:layout_height="wrap_content" - tools:ignore="SelectableText" /> + android:id="@+id/import_vpn_hint" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + tools:ignore="SelectableText" /> <Space - android:layout_width="match_parent" - android:layout_height="12sp" /> + android:layout_width="match_parent" + android:layout_height="12sp" /> <TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:text="@string/faq_hint" /> + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="@string/faq_hint" /> </LinearLayout> </LinearLayout> - <!-- <include layout="@layout/profile_list_fabs" /> --> + <!-- <include layout="@layout/profile_list_fabs" /> --> </FrameLayout> |