PluginInflateContainer.java revision de850bbcaa61c1874b803f2086443febbafd81a4
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14package com.android.systemui; 15 16import android.annotation.Nullable; 17import android.content.Context; 18import android.content.res.TypedArray; 19import android.util.AttributeSet; 20import android.util.Log; 21import android.view.View; 22 23import com.android.systemui.plugins.PluginListener; 24import com.android.systemui.plugins.PluginManager; 25import com.android.systemui.plugins.ViewProvider; 26 27/** 28 * Define an interface or abstract class as follows that includes the 29 * version and action. 30 * 31 * public interface MyInterface { 32 * public static final String ACTION = 33 * "com.android.systemui.action.PLUGIN_MYINTERFACE"; 34 * 35 * public static final int VERSION = 1; 36 * 37 * void myImportantInterface(); 38 * } 39 * 40 * Then put in a PluginInflateContainer to use and specify the interface 41 * or class that will be implemented as viewType. The layout specified 42 * will be used by default and whenever a plugin is not present. 43 * 44 * <com.android.systemui.PluginInflateContainer 45 * android:id="@+id/some_id" 46 * android:layout_width="match_parent" 47 * android:layout_height="match_parent" 48 * android:layout="@layout/my_default_component" 49 * systemui:viewType="com.android.systemui.plugins.MyInterface" /> 50 */ 51public class PluginInflateContainer extends AutoReinflateContainer 52 implements PluginListener<ViewProvider> { 53 54 private static final String TAG = "PluginInflateContainer"; 55 56 private String mAction; 57 private int mVersion; 58 private View mPluginView; 59 60 public PluginInflateContainer(Context context, @Nullable AttributeSet attrs) { 61 super(context, attrs); 62 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PluginInflateContainer); 63 String viewType = a.getString(R.styleable.PluginInflateContainer_viewType); 64 try { 65 Class c = Class.forName(viewType); 66 mAction = (String) c.getDeclaredField("ACTION").get(null); 67 mVersion = (int) c.getDeclaredField("VERSION").get(null); 68 } catch (Exception e) { 69 Log.d(TAG, "Problem getting class info " + viewType, e); 70 mAction = null; 71 mVersion = 0; 72 } 73 } 74 75 @Override 76 protected void onAttachedToWindow() { 77 super.onAttachedToWindow(); 78 if (mAction != null) { 79 Dependency.get(PluginManager.class).addPluginListener(mAction, this, mVersion); 80 } 81 } 82 83 @Override 84 protected void onDetachedFromWindow() { 85 super.onDetachedFromWindow(); 86 if (mAction != null) { 87 Dependency.get(PluginManager.class).removePluginListener(this); 88 } 89 } 90 91 @Override 92 protected void inflateLayoutImpl() { 93 if (mPluginView != null) { 94 addView(mPluginView); 95 } else { 96 super.inflateLayoutImpl(); 97 } 98 } 99 100 @Override 101 public void onPluginConnected(ViewProvider plugin, Context context) { 102 mPluginView = plugin.getView(); 103 inflateLayout(); 104 } 105 106 @Override 107 public void onPluginDisconnected(ViewProvider plugin) { 108 mPluginView = null; 109 inflateLayout(); 110 } 111} 112