1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.messaging.util;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.os.UserManager;
25import android.text.TextUtils;
26
27import com.android.messaging.Factory;
28import com.android.messaging.R;
29import com.android.messaging.datamodel.DataModel;
30import com.android.messaging.ui.conversation.ConversationActivity;
31import com.android.messaging.ui.conversationlist.ConversationListActivity;
32
33/**
34 * Utility class including logic to verify requirements to run Bugle and other activity startup
35 * logic. Called from base Bugle activity classes.
36 */
37public class BugleActivityUtil {
38
39    private static final int REQUEST_GOOGLE_PLAY_SERVICES = 0;
40
41    /**
42     * Determine if the requirements for the app to run are met. Log any Activity startup
43     * analytics.
44     * @param context
45     * @param activity is used to launch an error Dialog if necessary
46     * @return true if resume should continue normally. Returns false if some requirements to run
47     * are not met.
48     */
49    public static boolean onActivityResume(Context context, Activity activity) {
50        DataModel.get().onActivityResume();
51        Factory.get().onActivityResume();
52
53        // Validate all requirements to run are met
54        return checkHasSmsPermissionsForUser(context, activity);
55    }
56
57    /**
58     * Determine if the user doesn't have SMS permissions. This can happen if you are not the phone
59     * owner and the owner has disabled your SMS permissions.
60     * @param context is the Context used to resolve the user permissions
61     * @param activity is the Activity used to launch an error Dialog if necessary
62     * @return true if the user has SMS permissions, otherwise false.
63     */
64    private static boolean checkHasSmsPermissionsForUser(Context context, Activity activity) {
65        if (!OsUtil.isAtLeastL()) {
66            // UserManager.DISALLOW_SMS added in L. No multiuser phones before this
67            return true;
68        }
69        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
70        if (userManager.hasUserRestriction(UserManager.DISALLOW_SMS)) {
71            new AlertDialog.Builder(activity)
72                    .setMessage(R.string.requires_sms_permissions_message)
73                    .setCancelable(false)
74                    .setNegativeButton(R.string.requires_sms_permissions_close_button,
75                            new DialogInterface.OnClickListener() {
76                                @Override
77                                public void onClick(final DialogInterface dialog,
78                                        final int button) {
79                                    System.exit(0);
80                                }
81                            })
82                    .show();
83            return false;
84        }
85        return true;
86    }
87}
88
89