diff options
author | Christofer Ã…kersten <akersten@google.com> | 2019-03-01 00:27:39 -0800 |
---|---|---|
committer | Christofer Ã…kersten <akersten@google.com> | 2019-03-05 15:26:58 -0800 |
commit | c89934aa5096fdb725d3b68878888fd04425b2c3 (patch) | |
tree | 292e87d300241d776d2d73a77e8980603588b16b | |
parent | 897927296e3dea8b3a309233b3ea1afa3e72df0f (diff) | |
download | UniversalMediaPlayer-c89934aa5096fdb725d3b68878888fd04425b2c3.tar.gz |
Add permission request UX
Bug: 123328776
Test: manual
Change-Id: I66eb8ed2f3f31ed95050860154f0fc1aa1a562ff
-rw-r--r-- | java/com/android/pump/activity/PumpActivity.java | 63 | ||||
-rw-r--r-- | java/com/android/pump/fragment/PermissionFragment.java | 48 | ||||
-rw-r--r-- | java/com/android/pump/util/Permissions.java | 69 | ||||
-rw-r--r-- | res/layout/fragment_permission.xml | 76 |
4 files changed, 215 insertions, 41 deletions
diff --git a/java/com/android/pump/activity/PumpActivity.java b/java/com/android/pump/activity/PumpActivity.java index fd9a910..0a08053 100644 --- a/java/com/android/pump/activity/PumpActivity.java +++ b/java/com/android/pump/activity/PumpActivity.java @@ -16,7 +16,7 @@ package com.android.pump.activity; -import android.content.pm.PackageManager; +import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -28,8 +28,6 @@ import androidx.annotation.Nullable; import androidx.annotation.UiThread; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; @@ -45,9 +43,11 @@ import com.android.pump.fragment.GenreFragment; import com.android.pump.fragment.HomeFragment; import com.android.pump.fragment.MovieFragment; import com.android.pump.fragment.OtherFragment; +import com.android.pump.fragment.PermissionFragment; import com.android.pump.fragment.PlaylistFragment; import com.android.pump.fragment.SeriesFragment; import com.android.pump.util.Globals; +import com.android.pump.util.Permissions; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener; import com.google.android.material.tabs.TabLayout; @@ -55,12 +55,12 @@ import com.google.android.material.tabs.TabLayout; @UiThread public class PumpActivity extends AppCompatActivity implements OnNavigationItemSelectedListener { private static final int REQUIRED_PERMISSIONS_REQUEST_CODE = 42; - private static final String[] REQUIRED_PERMISSIONS = { - android.Manifest.permission.INTERNET, - android.Manifest.permission.READ_EXTERNAL_STORAGE, - android.Manifest.permission.WRITE_EXTERNAL_STORAGE - }; + // TODO Remove ugly PERMISSION_PAGES hack + private static final Pages PERMISSION_PAGES = + new Pages(R.id.menu_home, new Page[] { + new Page(PermissionFragment::newInstance, "Permission") + }); private static final Pages[] PAGES_LIST = { new Pages(R.id.menu_home, new Page[] { new Page(HomeFragment::newInstance, "Home") @@ -91,6 +91,10 @@ public class PumpActivity extends AppCompatActivity implements OnNavigationItemS private TabLayout mTabLayout; private BottomNavigationView mBottomNavigationView; + public static void requestPermissions(@NonNull Activity activity) { + Permissions.requestMissingPermissions(activity, REQUIRED_PERMISSIONS_REQUEST_CODE); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -111,7 +115,7 @@ public class PumpActivity extends AppCompatActivity implements OnNavigationItemS mViewPager.setAdapter(mActivityPagerAdapter); mTabLayout.setupWithViewPager(mViewPager); - if (!requestMissingPermissions()) { + if (!Permissions.isMissingPermissions(this)) { initialize(); } } @@ -133,6 +137,12 @@ public class PumpActivity extends AppCompatActivity implements OnNavigationItemS @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { + // TODO Remove ugly hack + if (item.getItemId() == R.id.menu_home && Permissions.isMissingPermissions(this)) { + selectPages(item.getTitle(), PERMISSION_PAGES); + return true; + } + for (Pages pages : PAGES_LIST) { if (pages.id == item.getItemId()) { selectPages(item.getTitle(), pages); @@ -146,20 +156,10 @@ public class PumpActivity extends AppCompatActivity implements OnNavigationItemS public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUIRED_PERMISSIONS_REQUEST_CODE) { - boolean granted = true; - if (grantResults.length == 0) { - granted = false; - } else { - for (int grantResult : grantResults) { - if (grantResult != PackageManager.PERMISSION_GRANTED) { - granted = false; - } - } - } - if (!granted) { - finish(); - } else { + if (Permissions.isGranted(permissions, grantResults)) { initialize(); + // TODO Remove ugly hack + mBottomNavigationView.setSelectedItemId(R.id.menu_home); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); @@ -170,25 +170,6 @@ public class PumpActivity extends AppCompatActivity implements OnNavigationItemS Globals.getMediaDb(this).load(); } - private boolean requestMissingPermissions() { - if (isMissingPermissions()) { - ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, - REQUIRED_PERMISSIONS_REQUEST_CODE); - return true; - } - return false; - } - - private boolean isMissingPermissions() { - for (String permission : REQUIRED_PERMISSIONS) { - if (ContextCompat.checkSelfPermission(this, permission) - != PackageManager.PERMISSION_GRANTED) { - return true; - } - } - return false; - } - private void selectPages(@NonNull CharSequence title, @NonNull Pages pages) { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { diff --git a/java/com/android/pump/fragment/PermissionFragment.java b/java/com/android/pump/fragment/PermissionFragment.java new file mode 100644 index 0000000..0e4aa68 --- /dev/null +++ b/java/com/android/pump/fragment/PermissionFragment.java @@ -0,0 +1,48 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.pump.fragment; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.UiThread; +import androidx.fragment.app.Fragment; + +import com.android.pump.R; +import com.android.pump.activity.PumpActivity; + +@UiThread +public class PermissionFragment extends Fragment { + public static @NonNull Fragment newInstance() { + return new PermissionFragment(); + } + + @Override + public @NonNull View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_permission, container, false); + + view.findViewById(R.id.fragment_permission_button) + .setOnClickListener((v) -> PumpActivity.requestPermissions(getActivity())); + + return view; + } +} diff --git a/java/com/android/pump/util/Permissions.java b/java/com/android/pump/util/Permissions.java new file mode 100644 index 0000000..9f1cd94 --- /dev/null +++ b/java/com/android/pump/util/Permissions.java @@ -0,0 +1,69 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.pump.util; + +import android.app.Activity; +import android.content.Context; +import android.content.pm.PackageManager; + +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; +import androidx.annotation.UiThread; +import androidx.core.app.ActivityCompat; + +@UiThread +public final class Permissions { + private static final String[] REQUIRED_PERMISSIONS = { + android.Manifest.permission.INTERNET, + android.Manifest.permission.READ_EXTERNAL_STORAGE, + android.Manifest.permission.WRITE_EXTERNAL_STORAGE + }; + + private Permissions() { } + + public static boolean isMissingPermissions(@NonNull Context context) { + for (String permission : REQUIRED_PERMISSIONS) { + if (ActivityCompat.checkSelfPermission(context, permission) + != PackageManager.PERMISSION_GRANTED) { + return true; + } + } + return false; + } + + public static boolean requestMissingPermissions(@NonNull Activity activity, + @IntRange(from = 0) int requestCode) { + if (isMissingPermissions(activity)) { + ActivityCompat.requestPermissions(activity, REQUIRED_PERMISSIONS, requestCode); + return true; + } + return false; + } + + public static boolean isGranted(@NonNull String[] permissions, @NonNull int[] grantResults) { + if (grantResults.length == 0) { + return false; + } else { + for (int grantResult : grantResults) { + if (grantResult != PackageManager.PERMISSION_GRANTED) { + return false; + } + } + } + return true; + } +} diff --git a/res/layout/fragment_permission.xml b/res/layout/fragment_permission.xml new file mode 100644 index 0000000..8a0908c --- /dev/null +++ b/res/layout/fragment_permission.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2019 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<androidx.constraintlayout.widget.ConstraintLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <com.android.pump.widget.UriImageView + android:id="@+id/fragment_permission_image" + android:layout_width="222dp" + android:layout_height="222dp" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toTopOf="@id/fragment_permission_text1" + app:layout_constraintVertical_chainStyle="packed" + app:srcCompat="@drawable/ic_placeholder"/> + + <androidx.appcompat.widget.AppCompatTextView + android:id="@+id/fragment_permission_text1" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginTop="24dp" + android:layout_marginStart="24dp" + android:layout_marginEnd="24dp" + android:gravity="center_horizontal" + android:textSize="24sp" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/fragment_permission_image" + app:layout_constraintBottom_toTopOf="@id/fragment_permission_text2" + android:text="Start the show"/> + + <androidx.appcompat.widget.AppCompatTextView + android:id="@+id/fragment_permission_text2" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginTop="24dp" + android:layout_marginStart="24dp" + android:layout_marginEnd="24dp" + android:gravity="center_horizontal" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/fragment_permission_text1" + app:layout_constraintBottom_toTopOf="@id/fragment_permission_button" + android:text= + "To play your videos and audios, allow access to the media files on your device."/> + + <com.google.android.material.button.MaterialButton + android:id="@+id/fragment_permission_button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="24dp" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/fragment_permission_text2" + app:layout_constraintBottom_toBottomOf="parent" + android:text="Allow Access"/> + +</androidx.constraintlayout.widget.ConstraintLayout> |