1 /* 2 * Copyright (C) 2010 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.gallery3d.photoeditor; 18 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.FrameLayout; 25 26 import java.util.HashMap; 27 import java.util.HashSet; 28 import java.util.Map.Entry; 29 30 /** 31 * View that holds a single child and could be recreated/restored after orientation changes. 32 */ 33 public abstract class RestorableView extends FrameLayout { 34 35 private final HashMap<Integer, Runnable> clickRunnables = new HashMap<Integer, Runnable>(); 36 private final HashSet<Integer> changedViews = new HashSet<Integer>(); 37 private final LayoutInflater inflater; 38 RestorableView(Context context, AttributeSet attrs)39 public RestorableView(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 42 } 43 childLayoutId()44 protected abstract int childLayoutId(); 45 recreateChildView()46 private void recreateChildView() { 47 if (getChildCount() != 0) { 48 removeAllViews(); 49 } 50 inflater.inflate(childLayoutId(), this, true); 51 } 52 53 @Override onFinishInflate()54 protected void onFinishInflate() { 55 super.onFinishInflate(); 56 recreateChildView(); 57 } 58 59 @Override onConfigurationChanged(Configuration newConfig)60 protected void onConfigurationChanged(Configuration newConfig) { 61 super.onConfigurationChanged(newConfig); 62 63 // Remember the removing child before recreating the child. 64 View view = getChildAt(0); 65 recreateChildView(); 66 67 // Restore its runnables and status of views that have been changed. 68 for (Entry<Integer, Runnable> entry : clickRunnables.entrySet()) { 69 setClickRunnable(entry.getKey(), entry.getValue()); 70 } 71 for (int id : changedViews) { 72 View changed = view.findViewById(id); 73 setViewEnabled(id, changed.isEnabled()); 74 setViewSelected(id, changed.isSelected()); 75 } 76 } 77 setClickRunnable(int id, final Runnable r)78 public void setClickRunnable(int id, final Runnable r) { 79 findViewById(id).setOnClickListener(new OnClickListener() { 80 81 @Override 82 public void onClick(View v) { 83 if (isEnabled()) { 84 r.run(); 85 } 86 } 87 }); 88 clickRunnables.put(id, r); 89 } 90 setViewEnabled(int id, boolean enabled)91 public void setViewEnabled(int id, boolean enabled) { 92 findViewById(id).setEnabled(enabled); 93 // Track views whose enabled status has been updated. 94 changedViews.add(id); 95 } 96 setViewSelected(int id, boolean selected)97 public void setViewSelected(int id, boolean selected) { 98 findViewById(id).setSelected(selected); 99 // Track views whose selected status has been updated. 100 changedViews.add(id); 101 } 102 } 103