• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.hardware.location;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.chre.flags.Flags;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.Objects;
27 
28 /**
29  * @hide
30  */
31 @SystemApi
32 public class MemoryRegion implements Parcelable{
33 
34     private int mSizeBytes;
35     private int mSizeBytesFree;
36     private boolean mIsReadable;
37     private boolean mIsWritable;
38     private boolean mIsExecutable;
39 
40     /**
41      * get the capacity of the memory region in bytes
42      *
43      * @return int - the memory capacity in bytes
44      */
getCapacityBytes()45     public int getCapacityBytes() {
46         return mSizeBytes;
47     }
48 
49     /**
50      * get the free capacity of the memory region in bytes
51      *
52      * @return int - free bytes
53      */
getFreeCapacityBytes()54     public int getFreeCapacityBytes() {
55         return mSizeBytesFree;
56     }
57 
58     /**
59      * Is the memory readable
60      *
61      * @return boolean - true if memory is readable, false otherwise
62      */
isReadable()63     public boolean isReadable() {
64         return mIsReadable;
65     }
66 
67     /**
68      * Is the memory writable
69      *
70      * @return boolean - true if memory is writable, false otherwise
71      */
isWritable()72     public boolean isWritable() {
73         return mIsWritable;
74     }
75 
76     /**
77      * Is the memory executable
78      *
79      * @return boolean - true if memory is executable, false
80      *         otherwise
81      */
isExecutable()82     public boolean isExecutable() {
83         return mIsExecutable;
84     }
85 
86     @NonNull
87     @Override
toString()88     public String toString() {
89         String mask = "";
90 
91         if (isReadable()) {
92             mask += "r";
93         } else {
94             mask += "-";
95         }
96 
97         if (isWritable()) {
98             mask += "w";
99         } else {
100             mask += "-";
101         }
102 
103         if (isExecutable()) {
104             mask += "x";
105         } else {
106             mask += "-";
107         }
108 
109         String retVal = "[ " + mSizeBytesFree + "/ " + mSizeBytes + " ] : " + mask;
110 
111         return retVal;
112     }
113 
114     @Override
equals(@ullable Object object)115     public boolean equals(@Nullable Object object) {
116         if (object == this) {
117             return true;
118         }
119 
120         boolean isEqual = false;
121         if (object instanceof MemoryRegion) {
122             MemoryRegion other = (MemoryRegion) object;
123             isEqual = (other.getCapacityBytes() == mSizeBytes)
124                     && (other.getFreeCapacityBytes() == mSizeBytesFree)
125                     && (other.isReadable() == mIsReadable)
126                     && (other.isWritable() == mIsWritable)
127                     && (other.isExecutable() == mIsExecutable);
128         }
129 
130         return isEqual;
131     }
132 
133     @Override
hashCode()134     public int hashCode() {
135         if (!Flags.fixApiCheck()) {
136             return super.hashCode();
137         }
138 
139         return Objects.hash(mSizeBytes, mSizeBytesFree, mIsReadable,
140                 mIsWritable, mIsExecutable);
141     }
142 
143     @Override
describeContents()144     public int describeContents() {
145         return 0;
146     }
147 
148     @Override
writeToParcel(Parcel dest, int flags)149     public void writeToParcel(Parcel dest, int flags) {
150         dest.writeInt(mSizeBytes);
151         dest.writeInt(mSizeBytesFree);
152         dest.writeInt(mIsReadable ? 1 : 0);
153         dest.writeInt(mIsWritable ? 1 : 0);
154         dest.writeInt(mIsExecutable ? 1 : 0);
155     }
156 
MemoryRegion(Parcel source)157     public MemoryRegion(Parcel source) {
158         mSizeBytes = source.readInt();
159         mSizeBytesFree = source.readInt();
160         mIsReadable = source.readInt() != 0;
161         mIsWritable = source.readInt() != 0;
162         mIsExecutable = source.readInt() != 0;
163     }
164 
165     public static final @android.annotation.NonNull Parcelable.Creator<MemoryRegion> CREATOR
166             = new Parcelable.Creator<MemoryRegion>() {
167         public MemoryRegion createFromParcel(Parcel in) {
168             return new MemoryRegion(in);
169         }
170 
171         public MemoryRegion[] newArray(int size) {
172             return new MemoryRegion[size];
173         }
174     };
175 
176 }
177