package android.support.v7.app;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.os.BuildCompat;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import java.util.Map;
import java.util.WeakHashMap;

public class SkinAppCompatDelegateImpl extends AppCompatDelegate {
    private static Map<Activity, AppCompatDelegate> sDelegateMap = new WeakHashMap<>();
    private final AppCompatDelegate mDelegate;

    public static AppCompatDelegate get(Activity activity, AppCompatCallback callback) {
        AppCompatDelegate delegate = sDelegateMap.get(activity);
        if (delegate == null) {
            delegate = new SkinAppCompatDelegateImpl(activity, callback);
            sDelegateMap.put(activity, delegate);
        }
        return delegate;
    }

    private SkinAppCompatDelegateImpl(Activity activity, AppCompatCallback callback) {
        mDelegate = AppCompatDelegate.create(activity, callback);
    }

    @Nullable
    @Override
    public ActionBar getSupportActionBar() {
        return mDelegate.getSupportActionBar();
    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        mDelegate.setSupportActionBar(toolbar);
    }

    @Override
    public MenuInflater getMenuInflater() {
        return mDelegate.getMenuInflater();
    }

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

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        mDelegate.onPostCreate(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mDelegate.onConfigurationChanged(newConfig);
    }

    @Override
    public void onStart() {
        mDelegate.onStart();
    }

    @Override
    public void onStop() {
        mDelegate.onStop();
    }

    @Override
    public void onPostResume() {
        mDelegate.onPostResume();
    }

    @Nullable
    @Override
    public View findViewById(int id) {
        return mDelegate.findViewById(id);
    }

    @Override
    public void setContentView(View v) {
        mDelegate.setContentView(v);
    }

    @Override
    public void setContentView(int resId) {
        mDelegate.setContentView(resId);
    }

    @Override
    public void setContentView(View v, ViewGroup.LayoutParams lp) {
        mDelegate.setContentView(v, lp);
    }

    @Override
    public void addContentView(View v, ViewGroup.LayoutParams lp) {
        mDelegate.addContentView(v, lp);
    }

    @Override
    public void setTitle(@Nullable CharSequence title) {
        mDelegate.setTitle(title);
    }

    @Override
    public void invalidateOptionsMenu() {
        mDelegate.invalidateOptionsMenu();
    }

    @Override
    public void onDestroy() {
        mDelegate.onDestroy();
    }

    @Nullable
    @Override
    public ActionBarDrawerToggle.Delegate getDrawerToggleDelegate() {
        return mDelegate.getDrawerToggleDelegate();
    }

    @Override
    public boolean requestWindowFeature(int featureId) {
        return mDelegate.requestWindowFeature(featureId);
    }

    @Override
    public boolean hasWindowFeature(int featureId) {
        return mDelegate.hasWindowFeature(featureId);
    }

    @Nullable
    @Override
    public ActionMode startSupportActionMode(@NonNull ActionMode.Callback callback) {
        return mDelegate.startSupportActionMode(callback);
    }

    @Override
    public void installViewFactory() {
        // ignore
    }

    @Override
    public View createView(@Nullable View parent, String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        return mDelegate.createView(parent, name, context, attrs);
    }

    @Override
    public void setHandleNativeActionModesEnabled(boolean enabled) {
        mDelegate.setHandleNativeActionModesEnabled(enabled);
    }

    @Override
    public boolean isHandleNativeActionModesEnabled() {
        return mDelegate.isHandleNativeActionModesEnabled();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        mDelegate.onSaveInstanceState(outState);
    }

    @Override
    public boolean applyDayNight() {
        return mDelegate.applyDayNight();
    }

    @Override
    public void setLocalNightMode(int mode) {
        mDelegate.setLocalNightMode(mode);
    }
}

@@ -133,33 +133,8 @@
 on;%0A
-import android.os.Build;%0A
 impo
@@ -246,50 +246,8 @@
 le;%0A
-import android.support.v4.os.BuildCompat;%0A
 impo
@@ -455,36 +455,8 @@
 oup;
-%0Aimport android.view.Window;
 %0A%0Aim
@@ -470,16 +470,20 @@
 va.util.
+Hash
 Map;%0Aimp
@@ -496,24 +496,16 @@
 va.util.
-WeakHash
 Map;%0A%0Apu
@@ -644,12 +644,8 @@
 new 
-Weak
 Hash
@@ -649,24 +649,25 @@
 ashMap%3C%3E();%0A
+%0A
     private 
@@ -700,16 +700,54 @@
 elegate;
+%0A    private final Activity mActivity;
 %0A%0A    pu
@@ -1171,32 +1171,62 @@
 ack callback) %7B%0A
+        mActivity = activity;%0A
         mDelegat
@@ -3161,24 +3161,64 @@
 nDestroy();%0A
+        sDelegateMap.remove(mActivity);%0A
     %7D%0A%0A    @

fix memory leak