[SetupWizardLib] Implement SetupWizardLayout

SetupWizardLayout is a generic implementation of a "chrome" for the
activity. The default implementation uses suw_template, but the client
is free to use an alternative layout using the android:layout
attribute.

SetupWizardLayoutHelper is designed to work with suw_template and its
variants. Custom templates can implement their own LayoutHelpers to
perform layout specific tasks on the template.

Bug: 18685749
Change-Id: Ib94602b62cd8faba41f171114651a54a90c5ee8d
diff --git a/library/res/values/attrs.xml b/library/res/values/attrs.xml
index 6006dc6..ad0ba36 100644
--- a/library/res/values/attrs.xml
+++ b/library/res/values/attrs.xml
@@ -25,4 +25,8 @@
         <attr name="suwHeader" format="reference" />
     </declare-styleable>
 
+    <declare-styleable name="SuwSetupWizardLayout">
+        <attr name="android:layout" />
+    </declare-styleable>
+
 </resources>
diff --git a/library/src/com/android/setupwizardlib/SetupWizardLayout.java b/library/src/com/android/setupwizardlib/SetupWizardLayout.java
index 6eea642..e329855 100644
--- a/library/src/com/android/setupwizardlib/SetupWizardLayout.java
+++ b/library/src/com/android/setupwizardlib/SetupWizardLayout.java
@@ -16,6 +16,99 @@
 
 package com.android.setupwizardlib;
 
-public class SetupWizardLayout {
-    // TODO: Implement this class
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+
+public class SetupWizardLayout extends FrameLayout {
+
+    private static final String TAG = "SetupWizardLayout";
+
+    /**
+     * The container of the actual content. This will be a view in the template, which child views
+     * will be added to when {@link #addView(android.view.View)} is called. This will be the layout
+     * in the template that has the ID of {@link #getContainerId()}. For the default implementation
+     * of SetupWizardLayout, that would be @id/suw_layout_content.
+     */
+    private ViewGroup mContainer;
+
+    public SetupWizardLayout(Context context) {
+        this(context, 0);
+    }
+
+    public SetupWizardLayout(Context context, int template) {
+        super(context);
+        inflateTemplate(template);
+    }
+
+    public SetupWizardLayout(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        if (attrs != null) {
+            final TypedArray a = context.obtainStyledAttributes(attrs,
+                    R.styleable.SuwSetupWizardLayout, defStyleAttr, 0);
+            final int template = a.getResourceId(
+                    R.styleable.SuwSetupWizardLayout_android_layout, 0);
+            inflateTemplate(template);
+            a.recycle();
+        } else {
+            inflateTemplate(0);
+        }
+    }
+
+    @Override
+    public void addView(View child, int index, ViewGroup.LayoutParams params) {
+        mContainer.addView(child, index, params);
+    }
+
+    private void addViewInternal(View child) {
+        super.addView(child, -1, generateDefaultLayoutParams());
+    }
+
+    private void inflateTemplate(int templateResource) {
+        final LayoutInflater inflater = LayoutInflater.from(getContext());
+        final View templateRoot = onInflateTemplate(inflater, templateResource);
+        addViewInternal(templateRoot);
+
+        mContainer = (ViewGroup) findViewById(getContainerId());
+        onTemplateInflated();
+    }
+
+    /**
+     * This method inflates the template. Subclasses can override this method to customize the
+     * template inflation, or change to a different default template. The root of the inflated
+     * layout should be returned, and not added to the view hierarchy.
+     *
+     * @param inflater A LayoutInflater to inflate the template.
+     * @param template The resource ID of the template to be inflated, or 0 if no template is
+     *                 specified.
+     * @return Root of the inflated layout.
+     */
+    protected View onInflateTemplate(LayoutInflater inflater, int template) {
+        if (template == 0) {
+            template = R.layout.suw_template;
+        }
+        return inflater.inflate(template, this, false);
+    }
+
+    /**
+     * This is called after the template has been inflated and added to the view hierarchy.
+     * Subclasses can implement this method to modify the template as necessary, such as caching
+     * views retrieved from findViewById, or other view operations that need to be done in code.
+     * You can think of this as {@link android.view.View#onFinishInflate()} but for inflation of the
+     * template instead of for child views.
+     */
+    protected void onTemplateInflated() {
+    }
+
+    protected int getContainerId() {
+        return R.id.suw_layout_content;
+    }
 }
diff --git a/library/src/com/android/setupwizardlib/util/SetupWizardLayoutHelper.java b/library/src/com/android/setupwizardlib/util/SetupWizardLayoutHelper.java
new file mode 100644
index 0000000..ad44e82
--- /dev/null
+++ b/library/src/com/android/setupwizardlib/util/SetupWizardLayoutHelper.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2015 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.setupwizardlib.util;
+
+import android.app.Activity;
+import android.view.View;
+import android.widget.TextView;
+
+import com.android.setupwizardlib.R;
+import com.android.setupwizardlib.SetupWizardLayout;
+
+/**
+ * This layout helper works with SetupWizardLayout and manages the templates for suw_template and
+ * its variants to perform some common operations.
+ *
+ * Layouts using custom templates can implement their own LayoutHelpers to perform layout specific
+ * tasks for the template.
+ */
+public class SetupWizardLayoutHelper {
+
+    /**
+     * Create a SetupWizardLayoutHelper from a view in the activity. If the viewId is not found, or
+     * if that view is not an instance of SetupWizardLayout, this method will return null.
+     *
+     * @return An instance of SetupWizardLayoutHelper, or null if viewId is not found or is not an
+     *         instance of SetupWizardLayout.
+     */
+    public static SetupWizardLayoutHelper from(Activity activity, int viewId) {
+        View view = activity.findViewById(viewId);
+        if (view instanceof SetupWizardLayout) {
+            return new SetupWizardLayoutHelper((SetupWizardLayout) view);
+        }
+        return null;
+    }
+
+    private SetupWizardLayout mLayout;
+
+    public SetupWizardLayoutHelper(SetupWizardLayout layout) {
+        mLayout = layout;
+    }
+
+    public SetupWizardLayout getLayout() {
+        return mLayout;
+    }
+
+    public void setHeaderText(int title) {
+        TextView titleView = (TextView) mLayout.findViewById(R.id.suw_layout_title);
+        if (titleView != null) {
+            titleView.setText(title);
+        }
+    }
+
+    public void setHeaderText(CharSequence title) {
+        TextView titleView = (TextView) mLayout.findViewById(R.id.suw_layout_title);
+        if (titleView != null) {
+            titleView.setText(title);
+        }
+    }
+}