/**
 * Copyright (C) 2016 The DirtyUnicorns Project
 * 
 * @author: Randall Rushing <randall.rushing@gmail.com>
 *
 * 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.
 * 
 * Core implementation of a inline navigation editor. Respond to many of the
 * same events BaseNavigationBar does and provide subclasses with information
 * regarding state changes
 *
 */

package com.android.systemui.navigation;

import com.android.systemui.navigation.BaseNavigationBar;
import com.android.systemui.navigation.Editor;
import com.android.systemui.R;

import com.android.internal.utils.du.Config.ActionConfig;
import com.android.internal.utils.du.DUSystemReceiver;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;

public abstract class BaseEditor implements Editor {
    public static final String INTENT_ACTION_EDIT_CLASS = "com.android.settings";
    public static final String INTENT_ACTION_EDIT_COMPONENT = "com.dirtyunicorns.dutweaks.ActionPickerDialogActivity";
    public static final String INTENT_ACTION_ICON_PICKER_COMPONENT = "com.dirtyunicorns.dutweaks.IconPickerActivity";
    public static final String INTENT_ICON_PICKER = "intent_icon_picker";
    public static final String INTENT_ACTION_PICKER = "intent_action_action_picker";
    public static final String INTENT_NAVBAR_EDIT = "intent_navbar_edit";
    public static final String EXTRA_NAVBAR_EDIT_RESET_LAYOUT = "extra_navbar_edit_reset_layout";
    public static int MODE_ON = 1;
    public static int MODE_OFF = 2;

    protected Context mContext;
    protected WindowManager mWindowManager;

    // we want any incoming hints (menu/ime/disabled) to automatcally
    // turn off editor. Except when we call these hint handling methods
    // as part of the editing process
    protected boolean mLockEditMode;
    private int mMode = MODE_OFF;
    private boolean mIsLandscape;
    private boolean mScreenOn;
    private boolean mKeyguardShowing;
    private boolean mScreenPinningOn;
    private BaseNavigationBar mHost;

    protected DUSystemReceiver mReceiver = new DUSystemReceiver() {

        @Override
        protected void onSecureReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.equals(INTENT_NAVBAR_EDIT, action)) {
                String doReset = intent.getStringExtra(EXTRA_NAVBAR_EDIT_RESET_LAYOUT);
                if (doReset != null) {
                    onResetLayout();
                } else {
                    final int mode = mMode;
                    if (mode == MODE_ON) {
                        changeEditMode(MODE_OFF);
                    } else {
                        if (isEditorAvailable()) {
                            changeEditMode(MODE_ON);
                        } else {
                            toastState(69);
                        }
                    }
                }
            } else if (TextUtils.equals(INTENT_ACTION_PICKER, action)) {
                int result = intent.getIntExtra("result", Activity.RESULT_CANCELED);
                if (result == Activity.RESULT_OK) {
                    String actionString = intent.getStringExtra("action_string");
                    ActionConfig config = intent.getParcelableExtra("action_config");
                    onActionPicked(actionString, config);
                }
            } else if (TextUtils.equals(INTENT_ICON_PICKER, action)) {
                int result = intent.getIntExtra("result", Activity.RESULT_CANCELED);
                if (result == Activity.RESULT_OK) {
                    String iconType = intent.getStringExtra("icon_data_type");
                    String iconPackage = intent.getStringExtra("icon_data_package");
                    String iconName = intent.getStringExtra("icon_data_name");
                    onIconPicked(iconType, iconPackage, iconName);
                }
            }
        }
    };

    public BaseEditor(BaseNavigationBar host) {
        mHost = host;
        mContext = host.getContext();
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        registerReceiver();
    }

    private void toastState(int state) {
        String txt;
        if (state == MODE_ON) {
            txt = mContext.getResources().getString(R.string.smartbar_editor_toast_starting);
        } else if (state == MODE_OFF) {
            txt = mContext.getResources().getString(R.string.smartbar_editor_toast_stopping);
        } else {
            txt = mContext.getResources().getString(R.string.smartbar_editor_toast_unavailable);
        }
        Toast.makeText(mContext, txt, Toast.LENGTH_SHORT).show();
    }

    public void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(INTENT_NAVBAR_EDIT);
        filter.addAction(INTENT_ACTION_PICKER);
        filter.addAction(INTENT_ICON_PICKER);
        mContext.registerReceiver(mReceiver, filter);
    }

    public void unregister() {
        mContext.unregisterReceiver(mReceiver);
    }

    protected abstract void onEditModeChanged(int mode);

    protected abstract void onPrepareToReorient();

    protected abstract void onReorient();

    protected void onActionPicked(String action, ActionConfig actionConfig) {}

    protected void onIconPicked(String type, String packageName, String iconName) {}

    protected void updateResources(Resources res) {}

    protected void onResetLayout() {}

    @Override
    public final void changeEditMode(int mode) {
        if (mLockEditMode || mMode == mode)
            return;
        mMode = mode;
        toastState(mode);
        onEditModeChanged(mode);
    }

    public boolean isEditorAvailable() {
        return !mKeyguardShowing
                && mScreenOn
                && !mScreenPinningOn
                && !mHost.mVertical
                && !mHost.mPulse.shouldDrawPulse()
                && !mHost.areAnyHintsActive();
    }

    @Override
    public final void prepareToReorient() {
        onPrepareToReorient();
    }

    @Override
    public final void reorient(boolean isLandscape) {
        mIsLandscape = isLandscape;
        if (mHost.mVertical) {
            changeEditMode(MODE_OFF);
        }
        onReorient();
    }

    @Override
    public void setKeyguardShowing(boolean showing) {
        mKeyguardShowing = showing;
        if (mKeyguardShowing) {
            changeEditMode(MODE_OFF);
        }
    }

    @Override
    public void notifyScreenOn(boolean screenOn) {
        mScreenOn = screenOn;
        if (!mScreenOn) {
            changeEditMode(MODE_OFF);
        }
    }

    @Override
    public void screenPinningStateChanged(boolean enabled) {
        mScreenPinningOn = enabled;
        if (mScreenPinningOn) {
            changeEditMode(MODE_OFF);
        }
    }

    protected boolean isLandscape() {
        return mIsLandscape;
    }

    public int getMode() {
        return mMode;
    }
}

@@ -1616,38 +1616,39 @@
  = %22com.
-dirtyunicorns.dutweaks
+android.settings.screwd
 .ActionP
@@ -1747,30 +1747,31 @@
 com.
-dirtyunicorns.dutweaks
+android.settings.screwd
 .Ico

Remove DUTweaks hardcoding