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

Commit 0ea7cebe authored by LuK1337's avatar LuK1337
Browse files

LineageParts: RIP profiles app groups

Change-Id: I1026b7166dcce47a1dcbb352b340a393aada279c
parent d4c35ea3
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
@@ -359,8 +359,6 @@
    <!-- Menu item for managing profiles -->
    <string name="profile_profiles_manage">Profiles</string>
    <string name="profile_profile_manage">Manage profile</string>
    <string name="profile_appgroups_manage">App groups</string>
    <string name="profile_appgroup_manage">Manage app group</string>

    <!-- Profile settings screen, section header for settings related to notification profiles -->
    <string name="profile_settings">Profile settings</string>
@@ -382,18 +380,6 @@
    <string name="soundtone_title">Notification tone</string>
    <string name="soundtone_summary" translatable="false">""</string>

    <!-- Title for application group setting screen -->
    <string name="profile_appgroups_title">App groups</string>
    <string name="profile_applist_title">Apps</string>
    <string name="profile_new_appgroup">New app group</string>
    <string name="profile_delete_appgroup">Delete this app group?</string>
    <string name="profile_appgroup_name_prompt">Enter a name for the new app group</string>
    <string name="profile_appgroup_name_title">Name</string>
    <string name="duplicate_appgroup_name">Duplicate app group name!</string>

    <!-- Add application dialog box title -->
    <string name="profile_choose_app">Choose app</string>

    <!-- Profiles - system settings -->
    <string name="profile_system_settings_title">System settings</string>
    <string name="profile_lockmode_title">Lock screen mode</string>

res/xml/appgroup_list.xml

deleted100644 → 0
+0 −22
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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.
-->

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="profile_appgroups_list"
        android:title="@string/profile_appgroups_title">

</PreferenceScreen>

res/xml/application_list.xml

deleted100644 → 0
+0 −29
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:key="general_section"
            android:title="@string/profile_appgroup_name_title">
    </PreferenceCategory>

    <PreferenceCategory
            android:key="applications_list"
            android:title="@string/profile_applist_title" >
    </PreferenceCategory>

</PreferenceScreen>
+0 −335
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *               2017-2020 The LineageOS 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 org.lineageos.lineageparts.profiles;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationGroup;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;

import org.lineageos.lineageparts.widget.PackageListAdapter;
import org.lineageos.lineageparts.widget.PackageListAdapter.PackageItem;
import org.lineageos.lineageparts.R;
import org.lineageos.lineageparts.SettingsPreferenceFragment;

import lineageos.app.ProfileManager;

public class AppGroupConfig extends SettingsPreferenceFragment
        implements Preference.OnPreferenceChangeListener {

    private static String TAG = "AppGroupConfig";

    private static final int DIALOG_APPS = 0;

    private static final int DELETE_CONFIRM = 1;

    private static final int DELETE_GROUP_CONFIRM = 2;

    public static final String PROFILE_SERVICE = "profile";

    private ListView mListView;

    private PackageManager mPackageManager;

    private NotificationGroup mNotificationGroup;

    private ProfileManager mProfileManager;

    private NamePreference mNamePreference;

    private static final int MENU_DELETE = Menu.FIRST;

    private static final int MENU_ADD = Menu.FIRST + 1;

    private PackageListAdapter mAppAdapter;

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

        if (savedInstanceState != null) {
            mPackageToDelete = savedInstanceState.getString("package_delete");
        }

        mProfileManager = ProfileManager.getInstance(getActivity());
        addPreferencesFromResource(R.xml.application_list);

        final Bundle args = getArguments();
        if (args != null) {
            mNotificationGroup = (NotificationGroup) args.getParcelable("NotificationGroup");
            mPackageManager = getPackageManager();
            mAppAdapter = new PackageListAdapter(getActivity());

            updatePackages();

            setHasOptionsMenu(true);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuItem delete = menu.add(0, MENU_DELETE, 0, R.string.profile_menu_delete_title)
                .setIcon(R.drawable.ic_actionbar_delete);
        delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        MenuItem addApplication = menu.add(0, MENU_ADD, 0, R.string.profiles_add)
                .setIcon(R.drawable.ic_menu_add)
                .setAlphabeticShortcut('a');
        addApplication.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_DELETE:
                deleteNotificationGroup();
                return true;
            case MENU_ADD:
                addNewApp();
                return true;
            default:
                return false;
        }
    }

    Preference mAddPreference;

    Preference mDeletePreference;

    private void updatePackages() {
        PreferenceScreen prefSet = getPreferenceScreen();

        // Add the General section
        PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("general_section");
        if (generalPrefs != null) {
            generalPrefs.removeAll();

            // Name preference
            mNamePreference = new NamePreference(getActivity(), mNotificationGroup.getName());
            mNamePreference.setOnPreferenceChangeListener(this);
            generalPrefs.addPreference(mNamePreference);
        }

        PreferenceGroup applicationsList = (PreferenceGroup) prefSet.findPreference("applications_list");
        if (applicationsList != null) {
            applicationsList.removeAll();
            for (String pkg : mNotificationGroup.getPackages()) {
                Preference pref = new Preference(getActivity());
                try {
                    PackageInfo group = mPackageManager.getPackageInfo(pkg, 0);
                    pref.setKey(group.packageName);
                    pref.setTitle(group.applicationInfo.loadLabel(mPackageManager));
                    Drawable icon = group.applicationInfo.loadIcon(mPackageManager);
                    pref.setIcon(icon);
                    pref.setSelectable(true);
                    pref.setPersistent(false);
                    applicationsList.addPreference(pref);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(0, R.string.profile_menu_delete_title, 0, R.string.profile_menu_delete_title);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        PackageItem selectedGroup = (PackageItem) mListView.getItemAtPosition(aMenuInfo.position);
        switch (item.getItemId()) {
            case R.string.profile_menu_delete_title:
                deleteAppFromGroup(selectedGroup);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void deleteAppFromGroup(PackageItem selectedGroup) {
        if (selectedGroup != null) {
            mNotificationGroup.removePackage(selectedGroup.packageName);
            updatePackages();
        }
    }

    @Override
    public void onPause() {
        if (mNotificationGroup != null) {
            mProfileManager.addNotificationGroup(mNotificationGroup);
        }
        super.onPause();
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference == mNamePreference) {
            String name = mNamePreference.getName().toString();
            if (!name.equals(mNotificationGroup.getName())) {
                if (!mProfileManager.notificationGroupExists(name)) {
                    mNotificationGroup.setName(name);
                } else {
                    mNamePreference.setName(mNotificationGroup.getName());
                    Toast.makeText(getActivity(), R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show();
                }
            }
        }
        return true;
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference instanceof Preference) {
            String deleteItem = preference.getKey();
            removeApp(deleteItem);
            return true;
        }
        return super.onPreferenceTreeClick(preference);
    }

    private void addNewApp() {
        showDialog(DIALOG_APPS);
        // TODO: switch to using the built in app list rather than dialog box?
    }

    private void removeApp(String key) {
        mPackageToDelete = key.toString();
        showDialog(DELETE_CONFIRM);
    }

    private void deleteNotificationGroup() {
        showDialog(DELETE_GROUP_CONFIRM);
    }

    @Override
    public Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final Dialog dialog;
        switch (id) {
            case DIALOG_APPS:
                Resources res = getResources();
                int paddingTop = res.getDimensionPixelOffset(R.dimen.package_list_padding_top);

                final ListView list = new ListView(getActivity());
                list.setAdapter(mAppAdapter);
                list.setDivider(null);
                list.setPadding(0, paddingTop, 0, 0);
                builder.setTitle(R.string.profile_choose_app);
                builder.setView(list);
                dialog = builder.create();
                list.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        PackageItem info = (PackageItem) parent.getItemAtPosition(position);
                        mNotificationGroup.addPackage(info.packageName);
                        updatePackages();
                        dialog.cancel();
                    }
                });
                break;
            case DELETE_CONFIRM:
                builder.setMessage(R.string.profile_app_delete_confirm);
                builder.setTitle(R.string.profile_menu_delete_title);
                builder.setIconAttribute(android.R.attr.alertDialogIcon);
                builder.setPositiveButton(android.R.string.yes,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doDelete();
                            }
                        });
                builder.setNegativeButton(android.R.string.no,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                dialog = builder.create();
                break;
            case DELETE_GROUP_CONFIRM:
                builder.setMessage(R.string.profile_delete_appgroup);
                builder.setTitle(R.string.profile_menu_delete_title);
                builder.setIconAttribute(android.R.attr.alertDialogIcon);
                builder.setPositiveButton(android.R.string.yes,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mProfileManager.removeNotificationGroup(mNotificationGroup);
                                mNotificationGroup = null;
                                finish();
                            }
                        });
                builder.setNegativeButton(android.R.string.no,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                dialog = builder.create();
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    String mPackageToDelete;

    private void doDelete() {
        mNotificationGroup.removePackage(mPackageToDelete);
        updatePackages();
    }

    @Override
    public void onSaveInstanceState(Bundle in) {
        super.onSaveInstanceState(in);
        in.putString("package_delete", mPackageToDelete);
    }
}
+0 −149
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 org.lineageos.lineageparts.profiles;

import android.annotation.Nullable;
import android.app.AlertDialog;
import android.app.NotificationGroup;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import org.lineageos.lineageparts.widget.FloatingActionButton;
import org.lineageos.lineageparts.R;
import org.lineageos.lineageparts.SettingsPreferenceFragment;
import org.lineageos.internal.util.ScreenType;

import java.util.UUID;

import lineageos.app.ProfileManager;

public class AppGroupList extends SettingsPreferenceFragment {

    private static final String TAG = "AppGroupSettings";

    private ProfileManager mProfileManager;

    // constant value that can be used to check return code from sub activity.
    private static final int APP_GROUP_CONFIG = 1;

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

        addPreferencesFromResource(R.xml.appgroup_list);
        mProfileManager = ProfileManager.getInstance(getActivity());
    }

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

        // On tablet devices remove the padding
        if (ScreenType.isTablet(getActivity())) {
            getListView().setPadding(0, 0, 0, 0);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final FloatingActionButton fab = getFloatingActionButton();
        fab.setImageResource(R.drawable.ic_menu_add_white);
        fab.setContentDescription(getString(R.string.profiles_add));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAppGroup();
            }
        });
        fab.setVisibility(View.VISIBLE);
    }

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

        // Add the existing app groups
        for (NotificationGroup group : mProfileManager.getNotificationGroups()) {
            PreferenceScreen pref = new PreferenceScreen(getActivity(), null);
            pref.setKey(group.getUuid().toString());
            pref.setTitle(group.getName());
            pref.setPersistent(false);
            appgroupList.addPreference(pref);
        }
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference instanceof PreferenceScreen) {
            NotificationGroup group = mProfileManager.getNotificationGroup(
                    UUID.fromString(preference.getKey()));
            editGroup(group);
        }
        return super.onPreferenceTreeClick(preference);
    }

    private void addAppGroup() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View content = inflater.inflate(R.layout.profile_name_dialog, null);
        final TextView prompt = (TextView) content.findViewById(R.id.prompt);
        final EditText entry = (EditText) content.findViewById(R.id.name);

        prompt.setText(R.string.profile_appgroup_name_prompt);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.profile_new_appgroup);
        builder.setView(content);

        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = entry.getText().toString();
                if (!mProfileManager.notificationGroupExists(name)) {
                    NotificationGroup newGroup = new NotificationGroup(name);
                    mProfileManager.addNotificationGroup(newGroup);

                    refreshList();
                } else {
                    Toast.makeText(getActivity(),
                            R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show();
                }
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void editGroup(NotificationGroup group) {
        Bundle args = new Bundle();
        args.putParcelable("NotificationGroup", group);

        startFragment(this, AppGroupConfig.class.getName(), R.string.profile_appgroup_manage,
                APP_GROUP_CONFIG, args);
    }
}
Loading