• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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 package com.android.ide.common.api;
17 
18 import com.android.annotations.NonNull;
19 import com.google.common.annotations.Beta;
20 
21 /**
22  * A {@link ResizePolicy} records state for whether a widget is resizable, and if so, in
23  * which directions
24  * <p>
25  * <b>NOTE: This is not a public or final API; if you rely on this be prepared
26  * to adjust your code for the next tools release.</b>
27  */
28 @Beta
29 public class ResizePolicy {
30     private static final int NONE = 0;
31     private static final int LEFT_EDGE = 1;
32     private static final int RIGHT_EDGE = 2;
33     private static final int TOP_EDGE = 4;
34     private static final int BOTTOM_EDGE = 8;
35     private static final int PRESERVE_RATIO = 16;
36 
37     // Aliases
38     private static final int HORIZONTAL = LEFT_EDGE | RIGHT_EDGE;
39     private static final int VERTICAL = TOP_EDGE | BOTTOM_EDGE;
40     private static final int ANY = HORIZONTAL | VERTICAL;
41 
42     // Shared objects for common policies
43 
44     private static final ResizePolicy sAny = new ResizePolicy(ANY);
45     private static final ResizePolicy sNone = new ResizePolicy(NONE);
46     private static final ResizePolicy sHorizontal = new ResizePolicy(HORIZONTAL);
47     private static final ResizePolicy sVertical = new ResizePolicy(VERTICAL);
48     private static final ResizePolicy sScaled = new ResizePolicy(ANY | PRESERVE_RATIO);
49 
50     private final int mFlags;
51 
52 
53     // Use factory methods to construct
ResizePolicy(int flags)54     private ResizePolicy(int flags) {
55         mFlags = flags;
56     }
57 
58     /**
59      * Returns true if this policy allows resizing in at least one direction
60      *
61      * @return true if this policy allows resizing in at least one direction
62      */
isResizable()63     public boolean isResizable() {
64         return (mFlags & ANY) != 0;
65     }
66 
67     /**
68      * Returns true if this policy allows resizing the top edge
69      *
70      * @return true if this policy allows resizing the top edge
71      */
topAllowed()72     public boolean topAllowed() {
73         return (mFlags & TOP_EDGE) != 0;
74     }
75 
76     /**
77      * Returns true if this policy allows resizing the right edge
78      *
79      * @return true if this policy allows resizing the right edge
80      */
rightAllowed()81     public boolean rightAllowed() {
82         return (mFlags & RIGHT_EDGE) != 0;
83     }
84 
85     /**
86      * Returns true if this policy allows resizing the bottom edge
87      *
88      * @return true if this policy allows resizing the bottom edge
89      */
bottomAllowed()90     public boolean bottomAllowed() {
91         return (mFlags & BOTTOM_EDGE) != 0;
92     }
93 
94     /**
95      * Returns true if this policy allows resizing the left edge
96      *
97      * @return true if this policy allows resizing the left edge
98      */
leftAllowed()99     public boolean leftAllowed() {
100         return (mFlags & LEFT_EDGE) != 0;
101     }
102 
103     /**
104      * Returns true if this policy requires resizing in an aspect-ratio preserving manner
105      *
106      * @return true if this policy requires resizing in an aspect-ratio preserving manner
107      */
isAspectPreserving()108     public boolean isAspectPreserving() {
109         return (mFlags & PRESERVE_RATIO) != 0;
110     }
111 
112     /**
113      * Returns a resize policy allowing resizing in any direction
114      *
115      * @return a resize policy allowing resizing in any direction
116      */
117     @NonNull
full()118     public static ResizePolicy full() {
119         return sAny;
120     }
121 
122     /**
123      * Returns a resize policy not allowing any resizing
124      *
125      * @return a policy which does not allow any resizing
126      */
127     @NonNull
none()128     public static ResizePolicy none() {
129         return sNone;
130     }
131 
132     /**
133      * Returns a resize policy allowing horizontal resizing only
134      *
135      * @return a policy which allows horizontal resizing only
136      */
137     @NonNull
horizontal()138     public static ResizePolicy horizontal() {
139         return sHorizontal;
140     }
141 
142     /**
143      * Returns a resize policy allowing vertical resizing only
144      *
145      * @return a policy which allows vertical resizing only
146      */
147     @NonNull
vertical()148     public static ResizePolicy vertical() {
149         return sVertical;
150     }
151 
152     /**
153      * Returns a resize policy allowing scaled / aspect-ratio preserving resizing only
154      *
155      * @return a resize policy allowing scaled / aspect-ratio preserving resizing only
156      */
157     @NonNull
scaled()158     public static ResizePolicy scaled() {
159         return sScaled;
160     }
161 
162     /**
163      * Returns a resize policy with the specified resizability along the edges and the
164      * given aspect ratio behavior
165      * @param top whether the top edge is resizable
166      * @param right whether the right edge is resizable
167      * @param bottom whether the bottom edge is resizable
168      * @param left whether the left edge is resizable
169      * @param preserve whether the policy requires the aspect ratio to be preserved
170      * @return a resize policy recording the constraints required by the parameters
171      */
172     @NonNull
create(boolean top, boolean right, boolean bottom, boolean left, boolean preserve)173     public static ResizePolicy create(boolean top, boolean right, boolean bottom, boolean left,
174             boolean preserve) {
175         int mask = NONE;
176         if (top) mask |= TOP_EDGE;
177         if (right) mask |= RIGHT_EDGE;
178         if (bottom) mask |= BOTTOM_EDGE;
179         if (left) mask |= LEFT_EDGE;
180         if (preserve) mask |= PRESERVE_RATIO;
181 
182         return new ResizePolicy(mask);
183     }
184 }
185