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