3131import android .widget .ImageView .ScaleType ;
3232
3333import com .android .internal .policy .impl .keyguard .KeyguardActivityLauncher .CameraWidgetInfo ;
34- import com .android .internal .R ;
3534
3635public class CameraWidgetFrame extends KeyguardWidgetFrame {
3736 private static final String TAG = CameraWidgetFrame .class .getSimpleName ();
@@ -49,10 +48,12 @@ interface Callbacks {
4948
5049 private View mWidgetView ;
5150 private long mLaunchCameraStart ;
51+ private boolean mRendered ;
5252
5353 private final Runnable mLaunchCameraRunnable = new Runnable () {
5454 @ Override
5555 public void run () {
56+ mLaunchCameraStart = SystemClock .uptimeMillis ();
5657 mActivityLauncher .launchCamera ();
5758 }};
5859
@@ -85,7 +86,9 @@ public static CameraWidgetFrame create(Context context, Callbacks callbacks,
8586 return null ;
8687
8788 ImageView preview = new ImageView (context );
88- preview .setLayoutParams (new FrameLayout .LayoutParams (FrameLayout .LayoutParams .MATCH_PARENT , FrameLayout .LayoutParams .MATCH_PARENT ));
89+ preview .setLayoutParams (new FrameLayout .LayoutParams (
90+ FrameLayout .LayoutParams .MATCH_PARENT ,
91+ FrameLayout .LayoutParams .MATCH_PARENT ));
8992 preview .setScaleType (ScaleType .FIT_CENTER );
9093 CameraWidgetFrame cameraWidgetFrame = new CameraWidgetFrame (context , callbacks , launcher );
9194 cameraWidgetFrame .addView (preview );
@@ -123,15 +126,31 @@ private static View inflateGenericWidgetView(Context context) {
123126 }
124127
125128 public void render () {
126- int width = getRootView ().getWidth ();
127- int height = getRootView ().getHeight ();
128- if (DEBUG ) Log .d (TAG , String .format ("render [%sx%s]" , width , height ));
129- Bitmap offscreen = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
130- Canvas c = new Canvas (offscreen );
131- mWidgetView .measure (MeasureSpec .makeMeasureSpec (width , MeasureSpec .EXACTLY ), MeasureSpec .makeMeasureSpec (height , MeasureSpec .EXACTLY ));
132- mWidgetView .layout (0 , 0 , width , height );
133- mWidgetView .draw (c );
134- ((ImageView )getChildAt (0 )).setImageBitmap (offscreen );
129+ if (mRendered ) return ;
130+
131+ try {
132+ int width = getRootView ().getWidth ();
133+ int height = getRootView ().getHeight ();
134+ if (DEBUG ) Log .d (TAG , String .format ("render [%sx%s] %s" ,
135+ width , height , Integer .toHexString (hashCode ())));
136+ if (width == 0 || height == 0 ) {
137+ return ;
138+ }
139+ Bitmap offscreen = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
140+ Canvas c = new Canvas (offscreen );
141+ mWidgetView .measure (
142+ MeasureSpec .makeMeasureSpec (width , MeasureSpec .EXACTLY ),
143+ MeasureSpec .makeMeasureSpec (height , MeasureSpec .EXACTLY ));
144+ mWidgetView .layout (0 , 0 , width , height );
145+ mWidgetView .draw (c );
146+ ((ImageView )getChildAt (0 )).setImageBitmap (offscreen );
147+ mRendered = true ;
148+ } catch (Throwable t ) {
149+ Log .w (TAG , "Error rendering camera widget" , t );
150+ removeAllViews ();
151+ View genericView = inflateGenericWidgetView (mContext );
152+ addView (genericView );
153+ }
135154 }
136155
137156 private void transitionToCamera () {
@@ -161,10 +180,10 @@ public void onWindowFocusChanged(boolean hasWindowFocus) {
161180 if (!hasWindowFocus ) {
162181 if (mLaunchCameraStart > 0 ) {
163182 long launchTime = SystemClock .uptimeMillis () - mLaunchCameraStart ;
164- if (DEBUG ) Log .d (TAG , String .format ("Camera took %s to launch" , launchTime ));
183+ if (DEBUG ) Log .d (TAG , String .format ("Camera took %sms to launch" , launchTime ));
165184 mLaunchCameraStart = 0 ;
185+ onCameraLaunched ();
166186 }
167- onCameraLaunched ();
168187 }
169188 }
170189
0 commit comments