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

Commit 8317c649 authored by Martin Brabham's avatar Martin Brabham Committed by Bruno Martins
Browse files

Make new menu entry to link to cLock widget settings.

[mikeioannina]: modified for O
[chrmhoffmann]: Fallback to cm lockclock prefs in case of upgrade from 14.1

Change-Id: I13ca3156c34eccdd8f60d8585281ae0585aac58e
parent d884f34f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -30,4 +30,7 @@
        <item>Nothing</item>
    </string-array>

    <!-- Setting title for accessing the cLock widget settings -->
    <string name="menu_item_widget_settings">Widget settings</string>

</resources>
+1 −0
Original line number Diff line number Diff line
@@ -21,4 +21,5 @@
    <item name="menu_item_sort" type="id" />
    <item name="menu_item_night_mode" type="id" />
    <item name="menu_item_settings" type="id" />
    <item name="menu_item_widget_settings" type="id" />
</resources>
+4 −2
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import com.android.deskclock.actionbarmenu.MenuItemControllerFactory;
import com.android.deskclock.actionbarmenu.NightModeMenuItemController;
import com.android.deskclock.actionbarmenu.OptionsMenuManager;
import com.android.deskclock.actionbarmenu.SettingsMenuItemController;
import com.android.deskclock.actionbarmenu.WidgetMenuItemController;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.data.DataModel.SilentSetting;
import com.android.deskclock.data.OnSilentSettingsListener;
@@ -153,7 +154,8 @@ public class DeskClock extends BaseActivity

        // Configure the menu item controllers add behavior to the toolbar.
        mOptionsMenuManager.addMenuItemController(
                new NightModeMenuItemController(this), new SettingsMenuItemController(this));
                new NightModeMenuItemController(this), new SettingsMenuItemController(this),
                new WidgetMenuItemController(this));
        mOptionsMenuManager.addMenuItemController(
                MenuItemControllerFactory.getInstance().buildMenuItemControllers(this));

+98 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2016 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.deskclock.actionbarmenu;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.android.deskclock.R;

import static android.view.Menu.NONE;

/**
 * {@link MenuItemController} for setting menu.
 */
public final class WidgetMenuItemController implements MenuItemController {

    private static final String LOG_TAG = "DeskClock";

    private static final String LC_PACKAGE = "org.lineageos.lockclock";
    private static final String LC_PACKAGE_FALLBACK = "com.cyanogenmod.lockclock";
    private static final String LC_ACTIVITY = LC_PACKAGE + ".preference.Preferences";
    private static final ComponentName sWidgetSettingComponentName = new ComponentName
            (LC_PACKAGE, LC_ACTIVITY);
    private static final ComponentName sWidgetSettingComponentNameFallback = new ComponentName
            (LC_PACKAGE_FALLBACK, LC_ACTIVITY);

    private static final int WIDGET_MENU_RES_ID = R.id.menu_item_widget_settings;
    private final Context mContext;

    public WidgetMenuItemController(Context context) {
        mContext = context;
    }

    @Override
    public int getId() {
        return WIDGET_MENU_RES_ID;
    }

    @Override
    public void onCreateOptionsItem(Menu menu) {
        menu.add(NONE, WIDGET_MENU_RES_ID, NONE, R.string.menu_item_widget_settings)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }

    @Override
    public void onPrepareOptionsItem(MenuItem item) {
        item.setVisible(isPackageInstalled(LC_PACKAGE));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent wsi = new Intent();
        wsi.setComponent(sWidgetSettingComponentName);
        try {
            mContext.startActivity(wsi);
        } catch (ActivityNotFoundException anfe) {
            wsi.setComponent(sWidgetSettingComponentNameFallback);
            mContext.startActivity(wsi);
        }
        return true;
    }

    private boolean isPackageInstalled(String pkg) {
        if (pkg == null) {
            return false;
        }
        try {
            PackageInfo pi = mContext.getPackageManager().getPackageInfo(pkg, 0);
            if (!pi.applicationInfo.enabled) {
                return false;
            } else {
                return true;
            }
        } catch (NameNotFoundException e) {
            return false;
        }
    }
}