Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26177a0b authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

Settings: merge ProfilesList into ProfilesSettings



ProfilesSettings previously had a viewpager with the profiles list, and
an app group list. Then we removed app groups, but the viewpager was
left over.

The main reason for getting rid of it now is to make sure that
refreshList() always works. Previously there was a small chance that
because it was in a viewpager, the preference may not have been attached
(at the time of the resetAll() dialog being shown and user pressing yes),
which caused the refresh method call to fail, since it needs access to
the preference screen, which is null at that time.

Change-Id: Ib9c3a27e3062660bd8a91998c20c15b4ffb4390b
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
(cherry picked from commit e3a73a77)
parent 06791cf7
Loading
Loading
Loading
Loading
+3 −22
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2014 The CyanogenMod Project
     Copyright (C) 2015 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -14,28 +14,9 @@
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

    </android.support.v4.view.ViewPager>

    <include layout="@layout/fab" />

    <TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/profile_empty_list_profiles_off"
        android:layout_centerInParent="true"
        android:gravity="center"/>
 No newline at end of file

</RelativeLayout>
+0 −118
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The CyanogenMod 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.settings.profiles;

import java.util.UUID;

import android.app.Profile;
import android.app.ProfileManager;
import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.android.internal.util.cm.ScreenType;

import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;

public class ProfilesList extends SettingsPreferenceFragment implements
        Preference.OnPreferenceChangeListener {
    static final String TAG = "ProfilesSettings";

    private ProfileManager mProfileManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.profiles_settings);
        mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshList();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Add a footer to avoid a situation where the FAB would cover the last
        // item's options in a non-scrollable listview.
        ListView listView = getListView();
        View footer = LayoutInflater.from(getActivity())
                .inflate(R.layout.empty_list_entry_footer, listView, false);
        listView.addFooterView(footer);
        listView.setFooterDividersEnabled(false);
        footer.setOnClickListener(null);
    }

    public void refreshList() {
        PreferenceScreen plist = getPreferenceScreen();
        plist.removeAll();

        // Get active profile, if null
        Profile prof = mProfileManager.getActiveProfile();
        String selectedKey = prof != null ? prof.getUuid().toString() : null;

        for (Profile profile : mProfileManager.getProfiles()) {
            Bundle args = new Bundle();
            args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile);
            args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false);

            ProfilesPreference ppref = new ProfilesPreference(this, args);
            ppref.setKey(profile.getUuid().toString());
            ppref.setTitle(profile.getName());
            ppref.setPersistent(false);
            ppref.setOnPreferenceChangeListener(this);
            ppref.setSelectable(true);
            ppref.setEnabled(true);

            if (TextUtils.equals(selectedKey, ppref.getKey())) {
                ppref.setChecked(true);
            }

            plist.addPreference(ppref);
        }
    }

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (newValue instanceof String) {
            setSelectedProfile((String) newValue);
            refreshList();
        }
        return true;
    }

    private void setSelectedProfile(String key) {
        try {
            UUID selectedUuid = UUID.fromString(key);
            mProfileManager.setActiveProfile(selectedUuid);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
    }
}
+91 −38
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.profiles;

import android.annotation.Nullable;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
@@ -30,9 +31,12 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.UserHandle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.support.v4.view.ViewPager;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
@@ -41,7 +45,9 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

@@ -53,8 +59,11 @@ import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.SubSettings;
import com.android.settings.cyanogenmod.BaseSystemSettingSwitchBar;

import java.util.UUID;

public class ProfilesSettings extends SettingsPreferenceFragment
        implements BaseSystemSettingSwitchBar.SwitchBarChangeCallback {
        implements BaseSystemSettingSwitchBar.SwitchBarChangeCallback,
        Preference.OnPreferenceChangeListener {
    private static final String TAG = "ProfilesSettings";

    public static final String EXTRA_PROFILE = "Profile";
@@ -69,9 +78,6 @@ public class ProfilesSettings extends SettingsPreferenceFragment
    private ProfileManager mProfileManager;
    private BaseSystemSettingSwitchBar mProfileEnabler;

    private ViewPager mViewPager;
    private TextView mEmptyText;
    private ProfilesPagerAdapter mAdapter;
    private View mAddProfileFab;
    private boolean mEnabled;

@@ -96,15 +102,43 @@ public class ProfilesSettings extends SettingsPreferenceFragment
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.profiles_settings);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContainer = container;
        View view = super.onCreateView(inflater, container, savedInstanceState);
        FrameLayout frameLayout = new FrameLayout(getActivity());
        mContainer = frameLayout;
        frameLayout.addView(view);
        return frameLayout;
    }

        View view = inflater.inflate(R.layout.profile_tabs, container, false);
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        mEmptyText = (TextView) view.findViewById(R.id.empty);
        mAddProfileFab = view.findViewById(R.id.floating_action_button);
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Add a footer to avoid a situation where the FAB would cover the last
        // item's options in a non-scrollable listview.
        ListView listView = getListView();
        View footer = LayoutInflater.from(getActivity())
                .inflate(R.layout.empty_list_entry_footer, listView, false);
        listView.addFooterView(footer);
        listView.setFooterDividersEnabled(false);
        footer.setOnClickListener(null);

        View v = LayoutInflater.from(getActivity())
                .inflate(R.layout.empty_textview, (ViewGroup) view, true);

        TextView emptyTextView = (TextView) v.findViewById(R.id.empty);
        listView.setEmptyView(emptyTextView);

        View fab = LayoutInflater.from(getActivity())
                .inflate(R.layout.fab, mContainer, true);
        mAddProfileFab = fab.findViewById(R.id.floating_action_button);
        mAddProfileFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
@@ -112,11 +146,6 @@ public class ProfilesSettings extends SettingsPreferenceFragment
                        addProfile();
                    }
                });

        mAdapter = new ProfilesPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);

        return view;
    }

    @Override
@@ -210,6 +239,9 @@ public class ProfilesSettings extends SettingsPreferenceFragment
                        mAdapter.refreshProfiles();
                        mProfileManager.setActiveProfile(
                                mProfileManager.getActiveProfile().getUuid());
                        dialog.dismiss();
                        refreshList();

                    }
                })
                .setNegativeButton(R.string.cancel, null)
@@ -224,8 +256,11 @@ public class ProfilesSettings extends SettingsPreferenceFragment
        activity.invalidateOptionsMenu();

        mAddProfileFab.setVisibility(mEnabled ? View.VISIBLE : View.GONE);
        mViewPager.setVisibility(mEnabled ? View.VISIBLE : View.GONE);
        mEmptyText.setVisibility(mEnabled ? View.GONE : View.VISIBLE);
        if (!mEnabled) {
            getPreferenceScreen().removeAll(); // empty it
        } else {
            refreshList();
        }
    }

    @Override
@@ -243,32 +278,50 @@ public class ProfilesSettings extends SettingsPreferenceFragment
        getActivity().sendBroadcastAsUser(u, UserHandle.ALL);
    }

    class ProfilesPagerAdapter extends FragmentStatePagerAdapter {
        Fragment[] frags = { new ProfilesList() };
        String[] titles = { getString(R.string.profile_profiles_manage) };
    public void refreshList() {
        PreferenceScreen plist = getPreferenceScreen();
        plist.removeAll();

        ProfilesPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        // Get active profile, if null
        Profile prof = mProfileManager.getActiveProfile();
        String selectedKey = prof != null ? prof.getUuid().toString() : null;

        @Override
        public Fragment getItem(int position) {
            return frags[position];
        for (Profile profile : mProfileManager.getProfiles()) {
            Bundle args = new Bundle();
            args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile);
            args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false);

            ProfilesPreference ppref = new ProfilesPreference(this, args);
            ppref.setKey(profile.getUuid().toString());
            ppref.setTitle(profile.getName());
            ppref.setPersistent(false);
            ppref.setOnPreferenceChangeListener(this);
            ppref.setSelectable(true);
            ppref.setEnabled(true);

            if (TextUtils.equals(selectedKey, ppref.getKey())) {
                ppref.setChecked(true);
            }

        @Override
        public int getCount() {
            return frags.length;
            plist.addPreference(ppref);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
    }

        public void refreshProfiles() {
            ((ProfilesList) frags[0]).refreshList();
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (newValue instanceof String) {
            setSelectedProfile((String) newValue);
            refreshList();
        }
        return true;
    }

    private void setSelectedProfile(String key) {
        try {
            UUID selectedUuid = UUID.fromString(key);
            mProfileManager.setActiveProfile(selectedUuid);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
    }

}