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

Commit 37131514 authored by Jason Monk's avatar Jason Monk Committed by Android (Google) Code Review
Browse files

Merge changes I52007c69,I6503947e,Icf677f4a,I2ae7ed61

* changes:
  Unit testing for fragments.
  Plugin fragment support
  Move QS to a fragment
  Fragments in SysUI!
parents bd199a2b 8852905b
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.
 */

package com.android.systemui.plugins;

import android.content.Context;
import android.view.View;

/**
 * Interface to deal with lack of multiple inheritance
 *
 * This interface is designed to be used as a base class for plugin interfaces
 * that need fragment methods. Plugins should not extend Fragment directly, so
 * plugins that are fragments should be extending PluginFragment, but in SysUI
 * these same versions should extend Fragment directly.
 *
 * Only methods that are on Fragment should be included here.
 */
public interface FragmentBase {
    View getView();
    Context getContext();
}
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.
 */

package com.android.systemui.plugins;

import android.annotation.Nullable;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.LayoutInflater;

public abstract class PluginFragment extends Fragment implements Plugin {

    private static final String KEY_PLUGIN_PACKAGE = "plugin_package_name";
    private Context mPluginContext;

    @Override
    public void onCreate(Context sysuiContext, Context pluginContext) {
        mPluginContext = pluginContext;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            Context sysuiContext = getContext();
            Context pluginContext = recreatePluginContext(sysuiContext, savedInstanceState);
            onCreate(sysuiContext, pluginContext);
        }
        if (mPluginContext == null) {
            throw new RuntimeException("PluginFragments must call super.onCreate("
                    + "Context sysuiContext, Context pluginContext)");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_PLUGIN_PACKAGE, getContext().getPackageName());
    }

    private Context recreatePluginContext(Context sysuiContext, Bundle savedInstanceState) {
        final String pkg = savedInstanceState.getString(KEY_PLUGIN_PACKAGE);
        try {
            ApplicationInfo appInfo = sysuiContext.getPackageManager().getApplicationInfo(pkg, 0);
            return PluginManager.getInstance(sysuiContext).getContext(appInfo, pkg);
        } catch (NameNotFoundException e) {
            throw new RuntimeException("Plugin with invalid package? " + pkg, e);
        }
    }

    @Override
    public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
        return super.getLayoutInflater(savedInstanceState).cloneInContext(mPluginContext);
    }

    /**
     * Should only be called after {@link Plugin#onCreate(Context, Context)}.
     */
    @Override
    public Context getContext() {
        return mPluginContext != null ? mPluginContext : super.getContext();
    }
}
+10 −2
Original line number Diff line number Diff line
@@ -158,7 +158,11 @@ public class PluginInstanceManager<T extends Plugin> {
                case PLUGIN_DISCONNECTED:
                    if (DEBUG) Log.d(TAG, "onPluginDisconnected");
                    mListener.onPluginDisconnected((T) msg.obj);
                    if (!(msg.obj instanceof PluginFragment)) {
                        // Only call onDestroy for plugins that aren't fragments, as fragments
                        // will get the onDestroy as part of the fragment lifecycle.
                        ((T) msg.obj).onDestroy();
                    }
                    break;
                default:
                    super.handleMessage(msg);
@@ -186,8 +190,12 @@ public class PluginInstanceManager<T extends Plugin> {
                    for (int i = mPlugins.size() - 1; i >= 0; i--) {
                        PluginInfo<T> plugin = mPlugins.get(i);
                        mListener.onPluginDisconnected(plugin.mPlugin);
                        if (!(plugin.mPlugin instanceof PluginFragment)) {
                            // Only call onDestroy for plugins that aren't fragments, as fragments
                            // will get the onDestroy as part of the fragment lifecycle.
                            plugin.mPlugin.onDestroy();
                        }
                    }
                    mPlugins.clear();
                    handleQueryPlugins(null);
                    break;
+35 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.HandlerThread;
@@ -26,6 +28,7 @@ import android.os.SystemProperties;
import android.util.ArrayMap;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.plugins.PluginInstanceManager.PluginContextWrapper;

import dalvik.system.PathClassLoader;

@@ -163,6 +166,16 @@ public class PluginManager extends BroadcastReceiver {
        return mParentClassLoader;
    }

    public Context getAllPluginContext(Context context) {
        return new PluginContextWrapper(context,
                new AllPluginClassLoader(context.getClassLoader()));
    }

    public Context getContext(ApplicationInfo info, String pkg) throws NameNotFoundException {
        ClassLoader classLoader = getClassLoader(info.sourceDir, pkg);
        return new PluginContextWrapper(mContext.createApplicationContext(info, 0), classLoader);
    }

    public static PluginManager getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PluginManager(context.getApplicationContext());
@@ -170,6 +183,28 @@ public class PluginManager extends BroadcastReceiver {
        return sInstance;
    }

    private class AllPluginClassLoader extends ClassLoader {
        public AllPluginClassLoader(ClassLoader classLoader) {
            super(classLoader);
        }

        @Override
        public Class<?> loadClass(String s) throws ClassNotFoundException {
            try {
                return super.loadClass(s);
            } catch (ClassNotFoundException e) {
                for (ClassLoader classLoader : mClassLoaders.values()) {
                    try {
                        return classLoader.loadClass(s);
                    } catch (ClassNotFoundException e1) {
                        // Will re-throw e if all fail.
                    }
                }
                throw e;
            }
        }
    }

    @VisibleForTesting
    public static class PluginInstanceManagerFactory {
        public <T extends Plugin> PluginInstanceManager createPluginInstanceManager(Context context,
@@ -180,7 +215,6 @@ public class PluginManager extends BroadcastReceiver {
        }
    }


    // This allows plugins to include any libraries or copied code they want by only including
    // classes from the plugin library.
    private static class ClassLoaderFilter extends ClassLoader {
+9 −5
Original line number Diff line number Diff line
@@ -25,17 +25,21 @@ import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public abstract class QSContainer extends FrameLayout {
import com.android.systemui.plugins.FragmentBase;

/**
 * Fragment that contains QS in the notification shade.  Most of the interface is for
 * handling the expand/collapsing of the view interaction.
 */
public interface QS extends FragmentBase {

    public static final String ACTION = "com.android.systemui.action.PLUGIN_QS";

    // This should be incremented any time this class or ActivityStarter or BaseStatusBarHeader
    // change in incompatible ways.
    public static final int VERSION = 3;
    public static final int VERSION = 4;

    public QSContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    String TAG = "QS";

    public abstract void setPanelView(HeightListener notificationPanelView);
    public abstract BaseStatusBarHeader getHeader();
Loading