Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2011 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 android.view;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.os.SystemClock;
     23 import android.util.AttributeSet;
     24 import android.util.Log;
     25 import android.view.View;
     26 
     27 /**
     28  * A View that will throw a RuntimeException if onAttachedToWindow and
     29  * onDetachedFromWindow is called in the wrong order for ViewAttachTest
     30  */
     31 public class ViewAttachView extends View {
     32     public static final String TAG = "OnAttachedTest";
     33     private boolean attached;
     34 
     35     public ViewAttachView(Context context, AttributeSet attrs, int defStyle) {
     36         super(context, attrs, defStyle);
     37         init(attrs, defStyle);
     38     }
     39 
     40     public ViewAttachView(Context context, AttributeSet attrs) {
     41         super(context, attrs);
     42         init(attrs, 0);
     43     }
     44 
     45     public ViewAttachView(Context context) {
     46         super(context);
     47         init(null, 0);
     48     }
     49 
     50     private void init(AttributeSet attrs, int defStyle) {
     51         SystemClock.sleep(2000);
     52     }
     53 
     54     @Override
     55     protected void onAttachedToWindow() {
     56         Log.d(TAG, "onAttachedToWindow");
     57         super.onAttachedToWindow();
     58         if (attached) {
     59             throw new RuntimeException("OnAttachedToWindow called more than once in a row");
     60         }
     61         attached = true;
     62     }
     63 
     64     @Override
     65     protected void onDetachedFromWindow() {
     66         Log.d(TAG, "onDetachedFromWindow");
     67         super.onDetachedFromWindow();
     68         if (!attached) {
     69             throw new RuntimeException(
     70                     "onDetachedFromWindowcalled without prior call to OnAttachedToWindow");
     71         }
     72         attached = false;
     73     }
     74 
     75     @Override
     76     protected void onDraw(Canvas canvas) {
     77         super.onDraw(canvas);
     78         canvas.drawColor(Color.BLUE);
     79     }
     80 }
     81