Calendar: Fix failure calendar on M Build on GTS:
Update Calendar with new permission api in SDK23
to fix failure calendar on M Build on GTS .
Change-Id: I5cfdbf099a7042e4c40d8681099c48f310222e80
CRs-Fixed: 972664
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 5673a3b..463685d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -38,7 +38,7 @@
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH.mail" />
- <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="18"></uses-sdk>
+ <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23"></uses-sdk>
<application android:name="CalendarApplication"
android:label="@string/app_label" android:icon="@mipmap/ic_launcher_calendar"
@@ -140,6 +140,10 @@
<activity android:name="CalendarSettingsActivity" android:label="@string/preferences_title"
android:theme="@android:style/Theme.Holo.Light"/>
+ <activity android:name="com.android.calendar.RequestPermissionsActivity" >
+ </activity>
+ <activity android:name="com.android.calendar.RequestPermissionsActivityBase" >
+ </activity>
<!-- Declarations for search -->
<!-- Make all activities a searchable context -->
diff --git a/res/values/strings.xml b/res/values/strings.xml
index caec61b..55a9968 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -16,7 +16,8 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- This is the label underneath the icon for Calendar -->
<string name="app_label">Calendar</string>
-
+ <!-- on permission of read_calendar -->
+ <string name="on_permission_read_calendar">You have disabled a required permission.</string>
<!-- Shared Labels. These labels are shared among the activities. -->
<skip/>
<!-- This is the label for the title or name of an event -->
diff --git a/src/com/android/calendar/AllInOneActivity.java b/src/com/android/calendar/AllInOneActivity.java
index 0e1feb4..b785d16 100644
--- a/src/com/android/calendar/AllInOneActivity.java
+++ b/src/com/android/calendar/AllInOneActivity.java
@@ -302,6 +302,9 @@
@Override
protected void onCreate(Bundle icicle) {
+ if (RequestPermissionsActivity.startPermissionActivity(this)) {
+ finish();
+ }
if (Utils.getSharedPreference(this, OtherPreferences.KEY_OTHER_1, false)) {
setTheme(R.style.CalendarTheme_WithActionBarWallpaper);
}
diff --git a/src/com/android/calendar/RequestPermissionsActivity.java b/src/com/android/calendar/RequestPermissionsActivity.java
new file mode 100644
index 0000000..504bb9c
--- /dev/null
+++ b/src/com/android/calendar/RequestPermissionsActivity.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package com.android.calendar;
+
+import android.Manifest.permission;
+import android.app.Activity;
+
+/**
+ * Activity that requests permissions needed for activities exported from canlendar.
+ */
+public class RequestPermissionsActivity extends RequestPermissionsActivityBase {
+
+ private static final String[] REQUIRED_PERMISSIONS = new String[] {
+ // "Calendar" group. Without this permission.
+ permission.READ_CALENDAR };
+
+ @Override
+ protected String[] getRequiredPermissions() {
+ return REQUIRED_PERMISSIONS;
+ }
+
+ @Override
+ protected String[] getDesiredPermissions() {
+ return new String[] { permission.READ_CALENDAR };
+ }
+
+ public static boolean startPermissionActivity(Activity activity) {
+ return startPermissionActivity(activity, REQUIRED_PERMISSIONS,
+ RequestPermissionsActivity.class);
+ }
+}
diff --git a/src/com/android/calendar/RequestPermissionsActivityBase.java b/src/com/android/calendar/RequestPermissionsActivityBase.java
new file mode 100644
index 0000000..751c12f
--- /dev/null
+++ b/src/com/android/calendar/RequestPermissionsActivityBase.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of The Linux Foundation nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package com.android.calendar;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+import android.os.Trace;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * Activity that asks the user for all {@link #getDesiredPermissions} if any of
+ * {@link #getRequiredPermissions} are missing.
+ *
+ * NOTE: As a result of b/22095159, this can behave oddly in the case where the
+ * final permission you are requesting causes an application restart.
+ */
+public abstract class RequestPermissionsActivityBase extends Activity {
+ public static final String PREVIOUS_ACTIVITY_INTENT = "previous_intent";
+ private static final int PERMISSIONS_REQUEST_ALL_PERMISSIONS = 1;
+
+ /**
+ * @return list of permissions that are needed in order for
+ * {@link #PREVIOUS_ACTIVITY_INTENT} to operate. You only need to
+ * return a single permission per permission group you care about.
+ */
+ protected abstract String[] getRequiredPermissions();
+
+ /**
+ * @return list of permissions that would be useful for
+ * {@link #PREVIOUS_ACTIVITY_INTENT} to operate. You only need to
+ * return a single permission per permission group you care about.
+ */
+ protected abstract String[] getDesiredPermissions();
+
+ private Intent mPreviousActivityIntent;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mPreviousActivityIntent = (Intent) getIntent().getExtras().get(
+ PREVIOUS_ACTIVITY_INTENT);
+
+ if (savedInstanceState == null) {
+ requestPermissions();
+ }
+ }
+
+ /**
+ * If any permissions the canlendar app needs are missing, open an Activity
+ * to prompt the user for these permissions. Moreover, finish the current
+ * activity.
+ *
+ * This is designed to be called inside
+ * {@link android.app.Activity#onCreate}
+ */
+ protected static boolean startPermissionActivity(Activity activity,
+ String[] requiredPermissions, Class<?> newActivityClass) {
+ if (!RequestPermissionsActivity.hasPermissions(activity,
+ requiredPermissions)) {
+ final Intent intent = new Intent(activity, newActivityClass);
+ intent.putExtra(PREVIOUS_ACTIVITY_INTENT, activity.getIntent());
+ activity.startActivity(intent);
+ activity.finish();
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode,
+ String permissions[], int[] grantResults) {
+ if (permissions != null && permissions.length > 0
+ && isAllGranted(permissions, grantResults)) {
+ mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
+ startActivity(mPreviousActivityIntent);
+ finish();
+ overridePendingTransition(0, 0);
+ } else {
+ Toast.makeText(this, R.string.on_permission_read_calendar, Toast.LENGTH_SHORT).show();
+ finish();
+ }
+ }
+
+ private boolean isAllGranted(String permissions[], int[] grantResult) {
+ for (int i = 0; i < permissions.length; i++) {
+ if (grantResult[i] != PackageManager.PERMISSION_GRANTED
+ && isPermissionRequired(permissions[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean isPermissionRequired(String p) {
+ return Arrays.asList(getRequiredPermissions()).contains(p);
+ }
+
+ private void requestPermissions() {
+ Trace.beginSection("requestPermissions");
+ try {
+ // Construct a list of missing permissions
+ final ArrayList<String> unsatisfiedPermissions = new ArrayList<>();
+ for (String permission : getDesiredPermissions()) {
+ if (checkSelfPermission(permission)
+ != PackageManager.PERMISSION_GRANTED) {
+ unsatisfiedPermissions.add(permission);
+ }
+ }
+ if (unsatisfiedPermissions.size() == 0) {
+ throw new RuntimeException("Request permission activity was called even"
+ + " though all permissions are satisfied.");
+ }
+ requestPermissions(
+ unsatisfiedPermissions.toArray(new String[unsatisfiedPermissions.size()]),
+ PERMISSIONS_REQUEST_ALL_PERMISSIONS);
+ } finally {
+ Trace.endSection();
+ }
+ }
+
+ protected static boolean hasPermissions(Context context,
+ String[] permissions) {
+ Trace.beginSection("hasPermission");
+ try {
+ for (String permission : permissions) {
+ if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
+ return false;
+ }
+ }
+ return true;
+ } finally {
+ Trace.endSection();
+ }
+ }
+}