11package com .lge .qcircle .template ;
22
33import android .app .Activity ;
4+ import android .content .ActivityNotFoundException ;
45import android .content .BroadcastReceiver ;
56import android .content .Context ;
67import android .content .Intent ;
@@ -56,6 +57,7 @@ public class QCircleTemplate {
5657 protected Context mContext = null ;
5758 protected BroadcastReceiver mReceiver = null ;
5859 protected Intent mFullscreenIntent = null ;
60+ protected IntentCreatorAsync mAsyncCreator = null ;
5961
6062 // Views
6163 protected TemplateType mLayoutType = TemplateType .CIRCLE_EMPTY ;
@@ -99,7 +101,7 @@ public QCircleTemplate(Context context) {
99101 */
100102 public QCircleTemplate (Context context , TemplateType type ) {
101103 mContext = context ;
102- registerIntentReceiver (); // register cover events
104+ registerIntentReceiver ();
103105 if (type == null )
104106 type = TemplateType .CIRCLE_EMPTY ;
105107 setTemplateType (type ); // set layout style
@@ -135,6 +137,17 @@ public void setFullscreenIntent(Intent intent) {
135137 Log .w (TAG , "The given intent is null" );
136138 }
137139 mFullscreenIntent = intent ;
140+ mAsyncCreator = null ;
141+ }
142+
143+ /**
144+ * sets the intent running in fullscreen when the cover is opened.
145+ *
146+ * @param creatorAsync will be called only when need the intent.
147+ */
148+ public void setFullscreenIntent (IntentCreatorAsync creatorAsync ) {
149+ mFullscreenIntent = null ;
150+ mAsyncCreator = creatorAsync ;
138151 }
139152
140153 /**
@@ -144,6 +157,7 @@ public void setFullscreenIntent(Intent intent) {
144157 * @return root view of the layout.
145158 */
146159 public View getView () {
160+ setQuickCircleWindowParam ();
147161 return mRootLayout ;
148162 }
149163
@@ -277,10 +291,25 @@ public void setTitle(View titleView, float heightRatio) {
277291 * has one.<br>
278292 * Note that this method does not create a button when the button already exists.
279293 */
280- public void setBackbutton () {
294+ public void setBackButton () {
295+ setBackButton (null );
296+ }
297+
298+ /**
299+ * sets a back button with callback.
300+ * <p>
301+ * It creates a back button on the bottom of the layout.
302+ * <p>
303+ * You do not need to implement an {@code onClickListener} for the button, because it already
304+ * has one.<br>
305+ * Note that this method does not create a button when the button already exists.
306+ * @param onClickListener Callback for back button.
307+ * <b>should be used for closing objects, like camera</b>
308+ */
309+ public void setBackButton (View .OnClickListener onClickListener ) {
281310 if (mBackButton == null ) {
282311 if (mContext != null )
283- mBackButton = new QCircleBackButton (mContext );
312+ mBackButton = new QCircleBackButton (mContext , onClickListener );
284313 else {
285314 Log .e (TAG , "Cannot create the back button: context is null" );
286315 return ;
@@ -289,6 +318,16 @@ public void setBackbutton() {
289318 }
290319 }
291320
321+ /**
322+ * Set the theme to the back button. Default is light.
323+ * @param isDark Is dark theme
324+ */
325+ public void setBackButtonTheme (boolean isDark ) {
326+ if (mBackButton != null ) {
327+ mBackButton .isDark (isDark );
328+ }
329+ }
330+
292331 /**
293332 * gets a layout with the given id.
294333 * <p>
@@ -303,15 +342,6 @@ public RelativeLayout getLayoutById(int id) {
303342 RelativeLayout result = null ;
304343 if (mContent != null && id > 0 ) {
305344 result = (RelativeLayout ) mContent .findViewById (id );
306- // if (result == null && mContent.findViewById(R.id.content_top) != null) {
307- // result = (RelativeLayout)mContent.findViewById(R.id.content_top).findViewById(id);
308- // } else if (result == null && mContentLayout != null) {
309- // result = (RelativeLayout)mContentLayout.findViewById(id);
310- // }
311- // for R.id.content
312- // if( result == null && ( mLayoutType == TemplateType.CIRCLE_COMPLEX || mLayoutType ==
313- // TemplateType.CIRCLE_EMPTY) )
314- // return mContent;
315345 }
316346 return result ;
317347 }
@@ -483,7 +513,7 @@ private void addTitleView(QCircleTitle titleView, float heightRatio) {
483513 /**
484514 * adds a button view to the layout.
485515 * <p>
486- * It is called by {@link QCircleTemplate#setBackbutton ()} to adjust the circle layout. The
516+ * It is called by {@link QCircleTemplate#setBackButton ()} to adjust the circle layout. The
487517 * button view is added on the bottom of the layout and the content window will be on the top of
488518 * the button view.
489519 *
@@ -639,15 +669,21 @@ public void onReceive(Context context, Intent intent) {
639669 // Gets the current state of the cover
640670 int quickCoverState = intent .getIntExtra (EXTRA_ACCESSORY_COVER_STATE ,
641671 EXTRA_ACCESSORY_COVER_OPENED );
642- // Log.d(TAG, "quickCoverState= " + quickCoverState);
643672 if (quickCoverState == EXTRA_ACCESSORY_COVER_CLOSED ) { // closed
644- // } else
645- // intenta.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
646- setQuickCircleWindowParam ();
673+ //setQuickCircleWindowParam();
647674 } else if (quickCoverState == EXTRA_ACCESSORY_COVER_OPENED ) { // opened
648675 if (mFullscreenIntent != null && mContext != null ) {
649- mContext .unregisterReceiver (this );
650676 mContext .startActivity (mFullscreenIntent );
677+ } else if (mContent != null && mAsyncCreator != null ) {
678+ Intent launching = mAsyncCreator .getIntent ();
679+ if (launching != null ) {
680+ try {
681+ mContext .startActivity (launching );
682+ } catch (ActivityNotFoundException e ) {
683+ // Package does not exist, ignore.
684+ e .printStackTrace ();
685+ }
686+ }
651687 }
652688 if (mContext instanceof Activity ) {
653689 ((Activity ) mContext ).finish ();
@@ -662,6 +698,14 @@ public void onReceive(Context context, Intent intent) {
662698 mContext .registerReceiver (mReceiver , filter );
663699 }
664700
701+ public void unregisterReceiver () {
702+ try {
703+ mContext .unregisterReceiver (mReceiver );
704+ } catch (Exception ignored ) {
705+ // Receiver not registered
706+ }
707+ }
708+
665709 /**
666710 * makes the circle shown even if the screen is locked.
667711 */
@@ -712,7 +756,7 @@ protected boolean setLayoutById(int id, View view) {
712756 */
713757 protected void loadCustomTemplate (int templateId ) {
714758 if (mContext != null && templateId > 0 ) {
715- View layoutView = (View ) ( (Activity ) mContext ).getLayoutInflater ().inflate (templateId ,
759+ View layoutView = ((Activity ) mContext ).getLayoutInflater ().inflate (templateId ,
716760 null );
717761 if (layoutView == null )
718762 Log .w (TAG , "Cannot set the custom layout: " + templateId );
@@ -724,4 +768,8 @@ protected void loadCustomTemplate(int templateId) {
724768 Log .w (TAG , "Cannot set the custom layout. Context is null" );
725769 }
726770 }
727- }
771+
772+ public static interface IntentCreatorAsync {
773+ Intent getIntent ();
774+ }
775+ }
0 commit comments