Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.notificationstudio.editor;
     18 
     19 import android.os.Build;
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 import android.widget.TextView;
     23 
     24 import com.android.notificationstudio.NotificationStudioActivity;
     25 import com.android.notificationstudio.R;
     26 import com.android.notificationstudio.model.EditableItem;
     27 import com.android.notificationstudio.model.EditableItemConstants;
     28 
     29 import java.util.HashMap;
     30 import java.util.Map;
     31 
     32 public class Editors implements EditableItemConstants {
     33 
     34     public interface Editor {
     35         Runnable bindEditor(View v, EditableItem item, Runnable afterChange);
     36     }
     37 
     38     private static final Map<Integer, Editor> EDITORS = editors();
     39     private static Runnable sUpdatePreset;
     40 
     41     private static Map<Integer, Editor> editors() {
     42         Map<Integer, Editor> editors = new HashMap<Integer, Editor>();
     43         editors.put(TYPE_RESOURCE_ID, new IconEditor());
     44         editors.put(TYPE_TEXT, new TextEditor());
     45         editors.put(TYPE_INT, new IntEditor());
     46         if (Build.VERSION.SDK_INT >= 14)  // switch 14, progress 14, uses chron 16
     47             editors.put(TYPE_BOOLEAN, new BooleanEditor());
     48         editors.put(TYPE_DROP_DOWN, new DropDownEditor());
     49         editors.put(TYPE_BITMAP, new BitmapEditor());
     50         if (Build.VERSION.SDK_INT >= 11) // fragments 11, when 11
     51             editors.put(TYPE_DATETIME, new DateTimeEditor());
     52         editors.put(TYPE_TEXT_LINES, new LinesEditor());
     53         return editors;
     54     }
     55 
     56     public static View newEditor(final NotificationStudioActivity activity,
     57             final ViewGroup parent, final EditableItem item) {
     58         final View editorView = activity.getLayoutInflater().inflate(R.layout.editable_item, null);
     59         ((TextView) editorView.findViewById(R.id.caption)).setText(item.getCaption(activity));
     60 
     61         // bind visibility
     62         editorView.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE);
     63         item.setVisibilityListener(new Runnable(){
     64             public void run() {
     65                 editorView.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE);
     66             }});
     67 
     68         // bind type-specific behavior
     69         Editor editor = EDITORS.get(item.getType());
     70         if (editor == null)
     71             return null;
     72         Runnable updater = editor.bindEditor(editorView, item, new Runnable() {
     73             public void run() {
     74                 if (item.equals(EditableItem.PRESET)) {
     75                     updateEditors(parent);
     76                 } else {
     77                     EditableItem.PRESET.setValue(PRESET_CUSTOM);
     78                     sUpdatePreset.run();
     79                 }
     80                 activity.refreshNotification();
     81             }});
     82 
     83         // store the updater as the view tag
     84         editorView.setTag(updater);
     85         if (item.equals(EditableItem.PRESET))
     86             sUpdatePreset = updater;
     87 
     88         return editorView;
     89     }
     90 
     91     private static void updateEditors(ViewGroup parent) {
     92         for (int i = 0; i < parent.getChildCount(); i++) {
     93             Object childTag = parent.getChildAt(i).getTag();
     94             if (childTag instanceof Runnable) {
     95                 ((Runnable) childTag).run();
     96             }
     97         }
     98     }
     99 
    100 }
    101