165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.users;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.RestrictionEntry;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.res.Resources;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserHandle;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserManager;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.provider.Settings.Secure;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class RestrictionUtils {
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final String [] sRestrictionKeys = {
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserManager.DISALLOW_SHARE_LOCATION,
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int [] sRestrictionTitles = {
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        R.string.restriction_location_enable_title,
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int [] sRestrictionDescriptions = {
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        R.string.restriction_location_enable_summary,
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * Returns the current user restrictions in the form of application
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * restriction entries.
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @return list of RestrictionEntry objects with user-visible text.
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Resources res = context.getResources();
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserManager um = UserManager.get(context);
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle userRestrictions = um.getUserRestrictions(user);
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (int i = 0; i < sRestrictionKeys.length; i++) {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            RestrictionEntry entry = new RestrictionEntry(
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    sRestrictionKeys[i],
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    !userRestrictions.getBoolean(sRestrictionKeys[i], false));
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            entry.setTitle(res.getString(sRestrictionTitles[i]));
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            entry.setDescription(res.getString(sRestrictionDescriptions[i]));
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            entry.setType(RestrictionEntry.TYPE_BOOLEAN);
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            entries.add(entry);
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return entries;
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static void setRestrictions(Context context, ArrayList<RestrictionEntry> entries,
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            UserHandle user) {
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserManager um = UserManager.get(context);
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle userRestrictions = um.getUserRestrictions(user);
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (RestrictionEntry entry : entries) {
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            userRestrictions.putBoolean(entry.getKey(), !entry.getSelectedState());
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (entry.getKey().equals(UserManager.DISALLOW_SHARE_LOCATION)
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    && !entry.getSelectedState()) {
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Secure.putIntForUser(context.getContentResolver(),
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF, user.getIdentifier());
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        um.setUserRestrictions(userRestrictions, user);
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static Bundle restrictionsToBundle(ArrayList<RestrictionEntry> entries) {
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        final Bundle bundle = new Bundle();
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (RestrictionEntry entry : entries) {
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (entry.getType() == RestrictionEntry.TYPE_BOOLEAN) {
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                bundle.putBoolean(entry.getKey(), entry.getSelectedState());
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else if (entry.getType() == RestrictionEntry.TYPE_MULTI_SELECT) {
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings());
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                bundle.putString(entry.getKey(), entry.getSelectedString());
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return bundle;
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
99