1 /* 2 * Copyright (C) 2015 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 com.android.camera.data; 18 19 import java.util.Locale; 20 21 /** 22 * Encapsulate latitude and longitude into a single location object. 23 * 24 * TODO: Add tests. Consider removing "ZERO" location and using UNKNOWN. 25 */ 26 public final class Location { 27 public static final Location UNKNOWN = new Location(Double.NaN, Double.NaN); 28 public static final Location ZERO = new Location(0.0, 0.0); 29 30 private final double mLatitude; 31 private final double mLongitude; 32 Location(double latitude, double longitude)33 private Location(double latitude, double longitude) { 34 mLatitude = latitude; 35 mLongitude = longitude; 36 } 37 getLatitude()38 public double getLatitude() { 39 return mLatitude; 40 } 41 getLongitude()42 public double getLongitude() { 43 return mLongitude; 44 } 45 getLocationString()46 public String getLocationString() { 47 return String.format(Locale.getDefault(), "%f, %f", mLatitude, 48 mLongitude); 49 } 50 isValid()51 public boolean isValid() { 52 return !this.equals(UNKNOWN) && !this.equals(ZERO) 53 && (mLatitude >= -90.0 && mLongitude <= 90.0) 54 && (mLongitude >= -180.0 && mLongitude <= 180.0); 55 } 56 57 @Override toString()58 public String toString() { 59 return "Location: " + getLocationString(); 60 } 61 62 @Override equals(Object o)63 public boolean equals(Object o) { 64 if (this == o) { 65 return true; 66 } 67 if (o == null || getClass() != o.getClass()) { 68 return false; 69 } 70 71 Location location = (Location) o; 72 73 if (Double.compare(location.mLatitude, mLatitude) != 0) { 74 return false; 75 } 76 if (Double.compare(location.mLongitude, mLongitude) != 0) { 77 return false; 78 } 79 80 return true; 81 } 82 83 @Override hashCode()84 public int hashCode() { 85 int result; 86 long temp; 87 temp = Double.doubleToLongBits(mLatitude); 88 result = (int) (temp ^ (temp >>> 32)); 89 temp = Double.doubleToLongBits(mLongitude); 90 result = 31 * result + (int) (temp ^ (temp >>> 32)); 91 return result; 92 } 93 from(double latitude, double longitude)94 public static Location from(double latitude, double longitude) { 95 if (Double.isNaN(latitude) || Double.isNaN(longitude) 96 || Double.isInfinite(latitude) || Double.isInfinite(longitude) 97 || (latitude == 0.0 && longitude == 0.0)) { 98 return UNKNOWN; 99 } 100 101 return new Location(latitude, longitude); 102 } 103 } 104