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 package com.android.loganalysis.item; 17 18 import org.json.JSONArray; 19 import org.json.JSONException; 20 import org.json.JSONObject; 21 22 import java.util.Arrays; 23 import java.util.Collection; 24 import java.util.HashSet; 25 import java.util.LinkedList; 26 import java.util.Set; 27 28 29 /** 30 * An {@link IItem} used to store location dumps. 31 */ 32 public class LocationDumpsItem implements IItem { 33 34 /** Constant for JSON output */ 35 public static final String LOCATION_CLIENTS = "LOCATION_CLIENTS"; 36 37 private Collection<LocationInfoItem> mLocationClients = 38 new LinkedList<LocationInfoItem>(); 39 40 public static class LocationInfoItem extends GenericItem { 41 /** Constant for JSON output */ 42 public static final String PACKAGE = "PACKAGE"; 43 /** Constant for JSON output */ 44 public static final String EFFECTIVE_INTERVAL = "EFFECTIVE_INTERVAL"; 45 /** Constant for JSON output */ 46 public static final String MIN_INTERVAL = "MIN_INTERVAL"; 47 /** Constant for JSON output */ 48 public static final String MAX_INTERVAL = "MAX_INTERVAL"; 49 /** Constant for JSON output */ 50 public static final String REQUEST_PRIORITY = "PRIORITY"; 51 /** Constant for JSON output */ 52 public static final String LOCATION_DURATION = "LOCATION_DURATION"; 53 54 private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList( 55 PACKAGE, EFFECTIVE_INTERVAL, MIN_INTERVAL, MAX_INTERVAL, REQUEST_PRIORITY, 56 LOCATION_DURATION )); 57 /** 58 * The constructor for {@link LocationInfoItem} 59 * 60 * @param packageName The package that requests location 61 * @param effective Effective interval of location request 62 * @param min Min interval of location request 63 * @param max Max interval of location request 64 * @param priority The priority of the request 65 * @param duration Duration of the request 66 */ LocationInfoItem(String packageName, int effective, int min, int max, String priority, int duration)67 public LocationInfoItem(String packageName, int effective, int min, int max, 68 String priority, int duration) { 69 super(ATTRIBUTES); 70 setAttribute(PACKAGE, packageName); 71 setAttribute(EFFECTIVE_INTERVAL, effective); 72 setAttribute(MIN_INTERVAL, min); 73 setAttribute(MAX_INTERVAL, max); 74 setAttribute(REQUEST_PRIORITY, priority); 75 setAttribute(LOCATION_DURATION, duration); 76 } 77 78 /** 79 * Get the name of the package 80 */ getPackage()81 public String getPackage() { 82 return (String) getAttribute(PACKAGE); 83 } 84 85 /** 86 * Get the effective location interval 87 */ getEffectiveInterval()88 public int getEffectiveInterval() { 89 return (int) getAttribute(EFFECTIVE_INTERVAL); 90 } 91 92 /** 93 * Get the min location interval 94 */ getMinInterval()95 public int getMinInterval() { 96 return (int) getAttribute(MIN_INTERVAL); 97 } 98 99 /** 100 * Get the max location interval 101 */ getMaxInterval()102 public int getMaxInterval() { 103 return (int) getAttribute(MAX_INTERVAL); 104 } 105 106 /** 107 * Get the priority of location request 108 */ getPriority()109 public String getPriority() { 110 return (String) getAttribute(REQUEST_PRIORITY); 111 } 112 113 /** 114 * Get the location duration 115 */ getDuration()116 public int getDuration() { 117 return (int) getAttribute(LOCATION_DURATION); 118 } 119 120 } 121 122 /** 123 * Add a location client {@link LocationDumpsItem}. 124 * 125 * @param packageName The package that requests location 126 * @param effective Effective interval of location request 127 * @param min Min interval of location request 128 * @param max Max interval of location request 129 * @param priority The priority of the request 130 * @param duration Duration of the request 131 */ addLocationClient(String packageName, int effective, int min, int max, String priority, int duration)132 public void addLocationClient(String packageName, int effective, int min, int max, 133 String priority, int duration) { 134 mLocationClients.add(new LocationInfoItem(packageName, effective, min, max, priority, 135 duration)); 136 } 137 getLocationClients()138 public Collection<LocationInfoItem> getLocationClients() { 139 return mLocationClients; 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override merge(IItem other)146 public IItem merge(IItem other) throws ConflictingItemException { 147 throw new ConflictingItemException("Location dumps items cannot be merged"); 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override isConsistent(IItem other)154 public boolean isConsistent(IItem other) { 155 return false; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override toJson()162 public JSONObject toJson() { 163 JSONObject object = new JSONObject(); 164 if (mLocationClients != null) { 165 try { 166 JSONArray locationClients = new JSONArray(); 167 for (LocationInfoItem locationClient : mLocationClients) { 168 locationClients.put(locationClient.toJson()); 169 } 170 object.put(LOCATION_CLIENTS, locationClients); 171 } catch (JSONException e) { 172 // Ignore 173 } 174 } 175 176 return object; 177 } 178 } 179