package Ġorg . bright ify . react ant . aut ol ayout . internal Ċ Ċ import Ġandroid . view . View Ċ import Ġorg . bright ify . react ant . aut ol ayout . Auto Layout Ċ import Ġorg . bright ify . react ant . aut ol ayout . Constraint Ċ import Ġorg . bright ify . react ant . aut ol ayout . Constraint Variable Ċ import Ġorg . bright ify . react ant . aut ol ayout . exception . Auto Layout NotFoundException Ċ import Ġorg . bright ify . react ant . aut ol ayout . exception . View Not Managed By Common Auto Layout Exception Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . solver . Background Solver Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . solver . Simp lex Solver Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . solver . Term Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . view . Intr insic Size Manager Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . view . Intr insic Size Ne cess ity Dec ider Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . view . View Constraints Ċ import Ġorg . bright ify . react ant . aut ol ayout . internal . view . Visibility Manager Ċ import Ġorg . bright ify . react ant . aut ol ayout . util . children Ċ import Ġorg . bright ify . react ant . aut ol ayout . util . description Ċ Ċ /** Ċ Ġ* Ġ Ġ@ author Ġ< a Ġhref =" mailto : fil ip @ bright ify . org "> Fil ip ĠDol nik </ a > Ċ Ġ*/ Ċ internal Ġclass ĠConstraint Manager Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġsolver Ġ= ĠBackground Solver ( Simp lex Solver ()) ĊĠĠĠ Ġprivate Ġval Ġconstraints Ġ= ĠHashMap < View , ĠHashSet < Constraint >>() ĊĠĠĠ Ġinternal Ġval Ġview Constraints Ġ= ĠHashMap < View , ĠView Constraints >() ĊĠĠĠ Ġprivate Ġval Ġintr insic Size Ne cess ity Dec ider Ġ= ĠIntr insic Size Ne cess ity Dec ider () ĊĊĠĠĠ Ġprivate Ġval Ġmanaged Views : ĠSet < View > ĊĠĠĠĠĠĠĠ Ġget () Ġ= Ġview Constraints . keys ĊĊĠĠĠ Ġval Ġall Constraints : ĠList < Constraint > ĊĠĠĠĠĠĠĠ Ġget () Ġ= Ġconstraints . flatMap Ġ{ Ġit . value Ġ} ĊĊĠĠĠ Ġfun Ġadd Constraint ( constraint : ĠConstraint ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( constraints [ constraint . view ]?. contains ( constraint ) Ġ== Ġtrue ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġif Ġ( verify Views Used By Constraint ( constraint )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsolver . add Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconstraint . is Managed Ġ= Ġtrue ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconstraints [ constraint . view ]?. add ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . add Constraint ( constraint ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġif Ġ(! managed Views . contains ( constraint . view )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠIllegalStateException (" View Ġ${ constraint . view . description } Ġis Ġnot Ġmanaged Ġby Ġcorrect ĠConstraint Manager .") ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠView Not Managed By Common Auto Layout Exception ( constraint . view , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġconstraint . constraint Items . map NotNull Ġ{ Ġit . right Variable ?. view Ġ}. first Ġ{ Ġ! managed Views . contains ( it ) Ġ}) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġremove Constraint ( constraint : ĠConstraint ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( constraints [ constraint . view ]?. remove ( constraint ) Ġ!= Ġtrue ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġsolver . remove Constraint ( constraint ) ĊĠĠĠĠĠĠĠ Ġconstraint . is Managed Ġ= Ġfalse ĊĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . remove Constraint ( constraint ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġadd Managed View ( view : ĠView ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( managed Views . contains ( view )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġconstraints [ view ] Ġ= ĠHashSet () ĊĠĠĠĠĠĠĠ Ġval Ġconstraints Ġ= ĠView Constraints ( view ) ĊĠĠĠĠĠĠĠ Ġview Constraints [ view ] Ġ= Ġconstraints ĊĠĠĠĠĠĠĠ Ġconstraints . initialize () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġremove Managed View ( view : ĠView ) Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ(! managed Views . contains ( view )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġconstraints . remove ( view )?. forEach Ġ{ Ġremove Constraint ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġnormalize Constraints () ĊĊĠĠĠĠĠĠĠ Ġview Constraints . remove ( view ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġjoin ( constraint Manager : ĠConstraint Manager ) Ġ{ ĊĠĠĠĠĠĠĠ Ġconstraints . put All ( constraint Manager . constraints ) ĊĠĠĠĠĠĠĠ Ġconstraint Manager . constraints . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . value . forEach Ġ{ Ġconstraint Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsolver . add Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . add Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġview Constraints . put All ( constraint Manager . view Constraints ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġsplit ( view : ĠView ): ĠConstraint Manager Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġleaving Views Ġ= ĠHashSet < View >() ĊĠĠĠĠĠĠĠ Ġfun Ġadd Recursive ( le aving : ĠView ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġleaving Views . add ( le aving ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( le aving Ġis ĠAuto Layout ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġleaving . children . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadd Recursive ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġadd Recursive ( view ) ĊĊĠĠĠĠĠĠĠ Ġval Ġnew Constraint Manager Ġ= ĠConstraint Manager () ĊĊĠĠĠĠĠĠĠ Ġval Ġleaving Size Managers Ġ= Ġview Constraints . filter Keys Ġ{ Ġleaving Views . contains ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġnew Constraint Manager . view Constraints . put All ( le aving Size Managers ) ĊĠĠĠĠĠĠĠ Ġleaving Size Managers . forEach Ġ{ Ġview Constraints . remove ( it . key ) Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġleaving Constraints Ġ= Ġconstraints . filter Keys Ġ{ Ġleaving Views . contains ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġnew Constraint Manager . constraints . put All ( le aving Constraints ) ĊĠĠĠĠĠĠĠ Ġnew Constraint Manager . normalize Constraints () ĊĠĠĠĠĠĠĠ Ġnew Constraint Manager . constraints . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . value . forEach Ġ{ Ġconstraint Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnew Constraint Manager . solver . add Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġleaving Views . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconstraints . remove ( it )?. forEach Ġ{ Ġconstraint Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsolver . remove Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġnormalize Constraints () ĊĊĠĠĠĠĠĠĠ Ġreturn Ġnew Constraint Manager ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġremove User View Constraints ( view : ĠView ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ(( constraints [ view ] Ġ?: ĠHashSet ()) Ġ- Ġ( view Constraints [ view ]?. used Constraints Ġ?: Ġempty Set ())). forEach Ġ{ Ġremove Constraint ( it ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun ĠgetValue For Variable ( variable : ĠConstraint Variable ): ĠDouble Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġresult Ġ= Ġ 0 . 0 ĊĠĠĠĠĠĠĠ ĠTerm ( variable ). base Terms . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresult Ġ+= Ġit . coefficient Ġ* Ġsolver . getValue For Variable ( it . variable ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġresult ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Visibility Manager ( view : ĠView ): ĠVisibility Manager Ġ= Ġview Constraints [ view ]?. visibility Manager Ġ?: Ġthrow ĠAuto Layout NotFoundException ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġview ) ĊĊĠĠĠ Ġfun Ġget Intr insic Size Manager ( view : ĠView ): ĠIntr insic Size Manager ? Ġ= Ġview Constraints [ view ]?. intrinsic Size Manager ĊĊĠĠĠ Ġfun Ġneeds Intr insic Width ( view : ĠView ): ĠBoolean Ġ= Ġget Intr insic Size Manager ( view ) Ġ!= Ġnull Ġ&& ĊĠĠĠĠĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . needs Intr insic Width ( view ) ĊĊĠĠĠ Ġfun Ġneeds Intr insic Height ( view : ĠView ): ĠBoolean Ġ= Ġget Intr insic Size Manager ( view ) Ġ!= Ġnull Ġ&& ĊĠĠĠĠĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . needs Intr insic Height ( view ) ĊĊĠĠĠ Ġfun Ġupdate Intr insic Size Ne cess ity Dec ider () Ġ{ ĊĠĠĠĠĠĠĠ Ġintr insic Size Ne cess ity Dec ider . update Views Need ing Intr insic Size ( view Constraints ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġverify Views Used By Constraint ( constraint : ĠConstraint ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġconstraint . constraint Items . all Ġ{ Ġconstraint Item Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmanaged Views . contains ( constraint Item . left Variable . view ) Ġ&& Ġconstraint Item . right Variable ?. let Ġ{ Ġmanaged Views . contains ( it . view ) Ġ} Ġ!= Ġfalse ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġnormalize Constraints () Ġ{ ĊĠĠĠĠĠĠĠ ĠHashMap ( constraints ). forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠHashSet ( it . value ). forEach Ġ{ Ġconstraint Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ(! verify Views Used By Constraint ( constraint )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġremove Constraint ( constraint ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ