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