17e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackbornpackage android.os; 27e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 3002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackbornimport android.util.Log; 49630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown 59630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brownimport java.util.Arrays; 69630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown 77e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn/** 87e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Describes the source of some work that may be done by someone else. 97e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Currently the public representation of what a work source is is not 107e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * defined; this is an opaque container. 117e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 127e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackbornpublic class WorkSource implements Parcelable { 13002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn static final String TAG = "WorkSource"; 145e45ee6752528791deb66b83d76250685de15d47Dianne Hackborn static final boolean DEBUG = false; 15002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 167e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int mNum; 177e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int[] mUids; 18002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn String[] mNames; 197e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 207e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 217e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Internal statics to avoid object allocations in some operations. 227e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * The WorkSource object itself is not thread safe, but we need to 237e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * hold sTmpWorkSource lock while working with these statics. 247e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 257e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn static final WorkSource sTmpWorkSource = new WorkSource(0); 267e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 277e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * For returning newbie work from a modification operation. 287e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 297e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn static WorkSource sNewbWork; 307e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 317e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * For returning gone work form a modification operation. 327e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 337e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn static WorkSource sGoneWork; 347e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 357e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 367e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Create an empty work source. 377e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 387e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource() { 397e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 0; 407e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 417e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 427e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 437e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Create a new WorkSource that is a copy of an existing one. 447e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * If <var>orig</var> is null, an empty WorkSource is created. 457e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 467e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource(WorkSource orig) { 477e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (orig == null) { 487e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 0; 497e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return; 507e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 517e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = orig.mNum; 527e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (orig.mUids != null) { 537e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = orig.mUids.clone(); 54002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = orig.mNames != null ? orig.mNames.clone() : null; 557e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 567e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = null; 57002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 587e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 597e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 607e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 617e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 627e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource(int uid) { 637e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 1; 647e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = new int[] { uid, 0 }; 65002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 66002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 67002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 68002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** @hide */ 69002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn public WorkSource(int uid, String name) { 70002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (name == null) { 71002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new NullPointerException("Name can't be null"); 72002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 73002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum = 1; 74002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids = new int[] { uid, 0 }; 75002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = new String[] { name, null }; 767e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 777e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 787e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn WorkSource(Parcel in) { 797e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = in.readInt(); 807e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = in.createIntArray(); 81002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = in.createStringArray(); 827e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 837e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 847e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 857e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public int size() { 867e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return mNum; 877e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 887e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 897e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 907e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public int get(int index) { 917e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return mUids[index]; 927e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 937e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 94002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** @hide */ 95002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn public String getName(int index) { 96002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return mNames != null ? mNames[index] : null; 97002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 98002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 997e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 1006ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie * Clear names from this WorkSource. Uids are left intact. 1016ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie * 1026ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie * <p>Useful when combining with another WorkSource that doesn't have names. 1036ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie * @hide 1046ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie */ 1056ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie public void clearNames() { 106a31510e47a0f0c2525702c2f10a85064322a28f2David Christie if (mNames != null) { 107a31510e47a0f0c2525702c2f10a85064322a28f2David Christie mNames = null; 108a31510e47a0f0c2525702c2f10a85064322a28f2David Christie // Clear out any duplicate uids now that we don't have names to disambiguate them. 109a31510e47a0f0c2525702c2f10a85064322a28f2David Christie int destIndex = 1; 110a31510e47a0f0c2525702c2f10a85064322a28f2David Christie int newNum = mNum; 111a31510e47a0f0c2525702c2f10a85064322a28f2David Christie for (int sourceIndex = 1; sourceIndex < mNum; sourceIndex++) { 112a31510e47a0f0c2525702c2f10a85064322a28f2David Christie if (mUids[sourceIndex] == mUids[sourceIndex - 1]) { 113a31510e47a0f0c2525702c2f10a85064322a28f2David Christie newNum--; 114a31510e47a0f0c2525702c2f10a85064322a28f2David Christie } else { 115a31510e47a0f0c2525702c2f10a85064322a28f2David Christie mUids[destIndex] = mUids[sourceIndex]; 116a31510e47a0f0c2525702c2f10a85064322a28f2David Christie destIndex++; 117a31510e47a0f0c2525702c2f10a85064322a28f2David Christie } 118a31510e47a0f0c2525702c2f10a85064322a28f2David Christie } 119a31510e47a0f0c2525702c2f10a85064322a28f2David Christie mNum = newNum; 120a31510e47a0f0c2525702c2f10a85064322a28f2David Christie } 1216ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie } 1226ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie 1236ab2284c98c08df68ed1ca8f7ac9748387ba6cb2David Christie /** 1247e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Clear this WorkSource to be empty. 1257e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 1267e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public void clear() { 1277e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 0; 1287e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 1297e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 13094838913abf6363532cd32b9c795917d808228ccJeff Brown @Override 13194838913abf6363532cd32b9c795917d808228ccJeff Brown public boolean equals(Object o) { 13294838913abf6363532cd32b9c795917d808228ccJeff Brown return o instanceof WorkSource && !diff((WorkSource)o); 13394838913abf6363532cd32b9c795917d808228ccJeff Brown } 13494838913abf6363532cd32b9c795917d808228ccJeff Brown 13594838913abf6363532cd32b9c795917d808228ccJeff Brown @Override 13694838913abf6363532cd32b9c795917d808228ccJeff Brown public int hashCode() { 13794838913abf6363532cd32b9c795917d808228ccJeff Brown int result = 0; 13894838913abf6363532cd32b9c795917d808228ccJeff Brown for (int i = 0; i < mNum; i++) { 13994838913abf6363532cd32b9c795917d808228ccJeff Brown result = ((result << 4) | (result >>> 28)) ^ mUids[i]; 14094838913abf6363532cd32b9c795917d808228ccJeff Brown } 141002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames != null) { 142002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn for (int i = 0; i < mNum; i++) { 143002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result = ((result << 4) | (result >>> 28)) ^ mNames[i].hashCode(); 144002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 145002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 14694838913abf6363532cd32b9c795917d808228ccJeff Brown return result; 14794838913abf6363532cd32b9c795917d808228ccJeff Brown } 14894838913abf6363532cd32b9c795917d808228ccJeff Brown 1497e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 1507e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Compare this WorkSource with another. 1517e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * @param other The WorkSource to compare against. 1527e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * @return If there is a difference, true is returned. 1537e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 1547e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public boolean diff(WorkSource other) { 1557e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int N = mNum; 1567e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (N != other.mNum) { 1577e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return true; 1587e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 1597e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int[] uids1 = mUids; 1607e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int[] uids2 = other.mUids; 161002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final String[] names1 = mNames; 162002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final String[] names2 = other.mNames; 1637e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn for (int i=0; i<N; i++) { 1647e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (uids1[i] != uids2[i]) { 1657e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return true; 1667e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 167002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (names1 != null && names2 != null && !names1[i].equals(names2[i])) { 168002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return true; 169002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 1707e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 1717e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return false; 1727e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 1737e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 1747e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 1757e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Replace the current contents of this work source with the given 1767e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * work source. If <var>other</var> is null, the current work source 1777e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * will be made empty. 1787e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 1797e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public void set(WorkSource other) { 1807e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (other == null) { 1817e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 0; 1827e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return; 1837e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 1847e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = other.mNum; 1857e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (other.mUids != null) { 1867e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (mUids != null && mUids.length >= mNum) { 1877e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn System.arraycopy(other.mUids, 0, mUids, 0, mNum); 1887e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 1897e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = other.mUids.clone(); 1907e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 191002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (other.mNames != null) { 192002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames != null && mNames.length >= mNum) { 193002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(other.mNames, 0, mNames, 0, mNum); 194002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 195002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = other.mNames.clone(); 196002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 197002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 198002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 199002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 2007e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 2017e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = null; 202002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 2037e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2047e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2057e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 2067e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 2077e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public void set(int uid) { 2087e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 1; 2097e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (mUids == null) mUids = new int[2]; 2107e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids[0] = uid; 211002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 212002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 213002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 214002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** @hide */ 215002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn public void set(int uid, String name) { 216002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (name == null) { 217002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new NullPointerException("Name can't be null"); 218002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 219002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum = 1; 220002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mUids == null) { 221002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids = new int[2]; 222002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = new String[2]; 223002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 224002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[0] = uid; 225002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames[0] = name; 2267e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2277e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 2287e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 2297e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource[] setReturningDiffs(WorkSource other) { 2307e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn synchronized (sTmpWorkSource) { 2317e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn sNewbWork = null; 2327e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn sGoneWork = null; 2337e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn updateLocked(other, true, true); 2347e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (sNewbWork != null || sGoneWork != null) { 2357e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn WorkSource[] diffs = new WorkSource[2]; 2367e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn diffs[0] = sNewbWork; 2377e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn diffs[1] = sGoneWork; 2387e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return diffs; 2397e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2407e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return null; 2417e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2427e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2437e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 2447e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** 2457e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * Merge the contents of <var>other</var> WorkSource in to this one. 2467e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * 2477e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * @param other The other WorkSource whose contents are to be merged. 2487e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * @return Returns true if any new sources were added. 2497e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn */ 2507e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public boolean add(WorkSource other) { 2517e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn synchronized (sTmpWorkSource) { 2527e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return updateLocked(other, false, false); 2537e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2547e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2557e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 2567e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 2577e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource addReturningNewbs(WorkSource other) { 2587e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn synchronized (sTmpWorkSource) { 2597e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn sNewbWork = null; 2607e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn updateLocked(other, false, true); 2617e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return sNewbWork; 2627e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2637e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 2647e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 2657e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 2667e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public boolean add(int uid) { 267002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNum <= 0) { 268002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = null; 269002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn insert(0, uid); 270002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return true; 271002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 272002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames != null) { 273002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Adding without name to named " + this); 274002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 275002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i = Arrays.binarySearch(mUids, 0, mNum, uid); 276002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Adding uid " + uid + " to " + this + ": binsearch res = " + i); 277002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i >= 0) { 278002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return false; 2797e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 280002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn insert(-i-1, uid); 281002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return true; 282002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 283002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 284002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** @hide */ 285002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn public boolean add(int uid, String name) { 286002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNum <= 0) { 287002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn insert(0, uid, name); 288002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return true; 289002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 290002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames == null) { 291002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Adding name to unnamed " + this); 292002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 293002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i; 294002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn for (i=0; i<mNum; i++) { 295002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mUids[i] > uid) { 296002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn break; 297002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 298002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mUids[i] == uid) { 299002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int diff = mNames[i].compareTo(name); 300002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (diff > 0) { 301002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn break; 302002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 303002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (diff == 0) { 304002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return false; 305002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 306002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 307002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 308002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn insert(i, uid, name); 309002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return true; 3107e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 3117e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 3127e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn /** @hide */ 3137e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource addReturningNewbs(int uid) { 3147e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn synchronized (sTmpWorkSource) { 3157e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn sNewbWork = null; 3167e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn sTmpWorkSource.mUids[0] = uid; 3177e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn updateLocked(sTmpWorkSource, false, true); 3187e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return sNewbWork; 3197e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 3207e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 3217e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 3227e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public boolean remove(WorkSource other) { 323002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNum <= 0 || other.mNum <= 0) { 324002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return false; 325002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 326002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames == null && other.mNames == null) { 327002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return removeUids(other); 328002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 329002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames == null) { 330002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Other " + other + " has names, but target " 331002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + this + " does not"); 332002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 333002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (other.mNames == null) { 334002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Target " + this + " has names, but other " 335002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + other + " does not"); 336002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 337002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return removeUidsAndNames(other); 338002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 339002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 340002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 341002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** @hide */ 342002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn public WorkSource stripNames() { 343002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNum <= 0) { 344002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return new WorkSource(); 345002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 346002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn WorkSource result = new WorkSource(); 347002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int lastUid = -1; 348002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn for (int i=0; i<mNum; i++) { 349002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int uid = mUids[i]; 350002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i == 0 || lastUid != uid) { 351002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result.add(uid); 352002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 353002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 354002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return result; 355002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 356002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 357002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private boolean removeUids(WorkSource other) { 3587e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int N1 = mNum; 3597e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int[] uids1 = mUids; 3607e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int N2 = other.mNum; 3617e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int[] uids2 = other.mUids; 3627e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn boolean changed = false; 363002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i1 = 0, i2 = 0; 364002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Remove " + other + " from " + this); 365002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (i1 < N1 && i2 < N2) { 366002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Step: target @ " + i1 + " of " + N1 + ", other @ " + i2 367002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " of " + N2); 3687e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (uids2[i2] == uids1[i1]) { 369002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 370002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + ": remove " + uids1[i1]); 3717e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn N1--; 372002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn changed = true; 37383770289f8eca5aa4c62647a6c4d9a5bd4c80a45Dianne Hackborn if (i1 < N1) System.arraycopy(uids1, i1+1, uids1, i1, N1-i1); 374002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 375002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else if (uids2[i2] > uids1[i1]) { 376002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip i1"); 377002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 378002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 379002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip i2"); 380002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 3817e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 382002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 383002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 384002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum = N1; 385002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 386002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return changed; 387002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 388002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 389002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private boolean removeUidsAndNames(WorkSource other) { 390002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int N1 = mNum; 391002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int[] uids1 = mUids; 392002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final String[] names1 = mNames; 393002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int N2 = other.mNum; 394002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int[] uids2 = other.mUids; 395002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final String[] names2 = other.mNames; 396002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn boolean changed = false; 397002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i1 = 0, i2 = 0; 398002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Remove " + other + " from " + this); 399002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (i1 < N1 && i2 < N2) { 400002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Step: target @ " + i1 + " of " + N1 + ", other @ " + i2 401002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " of " + N2 + ": " + uids1[i1] + " " + names1[i1]); 402002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (uids2[i2] == uids1[i1] && names2[i2].equals(names1[i1])) { 403002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 404002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + ": remove " + uids1[i1] + " " + names1[i1]); 405002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn N1--; 406002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn changed = true; 407002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 < N1) { 408002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(uids1, i1+1, uids1, i1, N1-i1); 409002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(names1, i1+1, names1, i1, N1-i1); 410002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 411002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 412002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else if (uids2[i2] > uids1[i1] 413002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn || (uids2[i2] == uids1[i1] && names2[i2].compareTo(names1[i1]) > 0)) { 414002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip i1"); 4157e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn i1++; 416002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 417002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip i2"); 418002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 4197e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 4207e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 4217e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 4227e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = N1; 4237e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 4247e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return changed; 4257e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 4267e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 4277e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn private boolean updateLocked(WorkSource other, boolean set, boolean returnNewbs) { 428002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames == null && other.mNames == null) { 429002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return updateUidsLocked(other, set, returnNewbs); 430002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 431002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNum > 0 && mNames == null) { 432002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Other " + other + " has names, but target " 433002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + this + " does not"); 434002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 435002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (other.mNum > 0 && other.mNames == null) { 436002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn throw new IllegalArgumentException("Target " + this + " has names, but other " 437002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + other + " does not"); 438002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 439002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return updateUidsAndNamesLocked(other, set, returnNewbs); 440002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 441002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 442002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 443002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private static WorkSource addWork(WorkSource cur, int newUid) { 444002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (cur == null) { 445002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return new WorkSource(newUid); 446002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 447002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn cur.insert(cur.mNum, newUid); 448002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return cur; 449002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 450002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 451002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private boolean updateUidsLocked(WorkSource other, boolean set, boolean returnNewbs) { 4527e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int N1 = mNum; 4537e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int[] uids1 = mUids; 4547e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int N2 = other.mNum; 4557e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn final int[] uids2 = other.mUids; 4567e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn boolean changed = false; 457002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i1 = 0, i2 = 0; 458002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Update " + this + " with " + other + " set=" + set 459002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " returnNewbs=" + returnNewbs); 460002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (i1 < N1 || i2 < N2) { 461002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Step: target @ " + i1 + " of " + N1 + ", other @ " + i2 462002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " of " + N2); 463002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 >= N1 || (i2 < N2 && uids2[i2] < uids1[i1])) { 4647e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn // Need to insert a new uid. 465002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 466002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + ": insert " + uids2[i2]); 4677e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn changed = true; 4687e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (uids1 == null) { 4697e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn uids1 = new int[4]; 4707e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn uids1[0] = uids2[i2]; 471002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else if (N1 >= uids1.length) { 4727e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int[] newuids = new int[(uids1.length*3)/2]; 4737e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (i1 > 0) System.arraycopy(uids1, 0, newuids, 0, i1); 4747e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (i1 < N1) System.arraycopy(uids1, i1, newuids, i1+1, N1-i1); 4757e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn uids1 = newuids; 4767e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn uids1[i1] = uids2[i2]; 4777e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 4787e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (i1 < N1) System.arraycopy(uids1, i1, uids1, i1+1, N1-i1); 4797e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn uids1[i1] = uids2[i2]; 4807e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 4817e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (returnNewbs) { 482002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn sNewbWork = addWork(sNewbWork, uids2[i2]); 4837e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 4847e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn N1++; 4857e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn i1++; 486002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 4877e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 4887e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (!set) { 4897e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn // Skip uids that already exist or are not in 'other'. 490002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip"); 491002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i2 < N2 && uids2[i2] == uids1[i1]) { 492002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 493002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 494002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 4957e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } else { 4967e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn // Remove any uids that don't exist in 'other'. 4977e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int start = i1; 498002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (i1 < N1 && (i2 >= N2 || uids2[i2] > uids1[i1])) { 499002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + ": remove " + uids1[i1]); 500002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn sGoneWork = addWork(sGoneWork, uids1[i1]); 5017e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn i1++; 5027e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5037e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (start < i1) { 504002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(uids1, i1, uids1, start, N1-i1); 5057e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn N1 -= i1-start; 5067e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn i1 = start; 5077e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5087e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn // If there is a matching uid, skip it. 509002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 < N1 && i2 < N2 && uids2[i2] == uids1[i1]) { 510002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1 + ": skip"); 5117e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn i1++; 512002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 5137e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5147e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5157e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5167e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5177e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 5187e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = N1; 5197e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = uids1; 5207e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 5217e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return changed; 5227e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 5237e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 524002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn /** 525002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn * Returns 0 if equal, negative if 'this' is before 'other', positive if 'this' is after 'other'. 526002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn */ 527002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private int compare(WorkSource other, int i1, int i2) { 528002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int diff = mUids[i1] - other.mUids[i2]; 529002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (diff != 0) { 530002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return diff; 531002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 532002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return mNames[i1].compareTo(other.mNames[i2]); 533002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 534002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 535002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private static WorkSource addWork(WorkSource cur, int newUid, String newName) { 536002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (cur == null) { 537002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return new WorkSource(newUid, newName); 538002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 539002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn cur.insert(cur.mNum, newUid, newName); 540002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return cur; 541002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 542002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 543002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private boolean updateUidsAndNamesLocked(WorkSource other, boolean set, boolean returnNewbs) { 544002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int N2 = other.mNum; 545002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn final int[] uids2 = other.mUids; 546002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn String[] names2 = other.mNames; 547002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn boolean changed = false; 548002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int i1 = 0, i2 = 0; 549002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Update " + this + " with " + other + " set=" + set 550002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " returnNewbs=" + returnNewbs); 551002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (i1 < mNum || i2 < N2) { 552002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Step: target @ " + i1 + " of " + mNum + ", other @ " + i2 553002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " of " + N2); 554002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int diff = -1; 555002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 >= mNum || (i2 < N2 && (diff=compare(other, i1, i2)) > 0)) { 556002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn // Need to insert a new uid. 557002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn changed = true; 558002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + mNum 559002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + ": insert " + uids2[i2] + " " + names2[i2]); 560002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn insert(i1, uids2[i2], names2[i2]); 561002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (returnNewbs) { 562002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn sNewbWork = addWork(sNewbWork, uids2[i2], names2[i2]); 563002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 564002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 565002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 566002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 567002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (!set) { 568002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + mNum + ": skip"); 569002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i2 < N2 && diff == 0) { 570002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 571002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 572002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 573002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 574002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn // Remove any uids that don't exist in 'other'. 575002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int start = i1; 576002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn while (diff < 0) { 577002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + ": remove " + mUids[i1] 578002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn + " " + mNames[i1]); 579002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn sGoneWork = addWork(sGoneWork, mUids[i1], mNames[i1]); 580002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 581002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 >= mNum) { 582002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn break; 583002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 584002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn diff = i2 < N2 ? compare(other, i1, i2) : -1; 585002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 586002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (start < i1) { 587002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, i1, mUids, start, mNum-i1); 588002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mNames, i1, mNames, start, mNum-i1); 589002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum -= i1-start; 590002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1 = start; 591002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 592002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn // If there is a matching uid, skip it. 593002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (i1 < mNum && diff == 0) { 594002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + mNum + ": skip"); 595002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i1++; 596002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn i2++; 597002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 598002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 599002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 600002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 601002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 602002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn return changed; 603002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 604002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn 605002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private void insert(int index, int uid) { 606002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (DEBUG) Log.d(TAG, "Insert in " + this + " @ " + index + " uid " + uid); 6077e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn if (mUids == null) { 6087e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = new int[4]; 6097e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids[0] = uid; 6107e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mNum = 1; 611002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else if (mNum >= mUids.length) { 6127e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn int[] newuids = new int[(mNum*3)/2]; 613002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index > 0) { 614002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, 0, newuids, 0, index); 615002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 616002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index < mNum) { 617002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, index, newuids, index+1, mNum-index); 618002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 6197e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn mUids = newuids; 620002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[index] = uid; 621002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum++; 622002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 623002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index < mNum) { 624002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, index, mUids, index+1, mNum-index); 625002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 626002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[index] = uid; 627002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum++; 6287e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 629002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 6307e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 631002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn private void insert(int index, int uid, String name) { 632002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mUids == null) { 633002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids = new int[4]; 634002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[0] = uid; 635002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = new String[4]; 636002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames[0] = name; 637002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum = 1; 638002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else if (mNum >= mUids.length) { 639002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn int[] newuids = new int[(mNum*3)/2]; 640002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn String[] newnames = new String[(mNum*3)/2]; 641002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index > 0) { 642002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, 0, newuids, 0, index); 643002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mNames, 0, newnames, 0, index); 644002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 645002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index < mNum) { 646002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, index, newuids, index+1, mNum-index); 647002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mNames, index, newnames, index+1, mNum-index); 648002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 649002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids = newuids; 650002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames = newnames; 651002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[index] = uid; 652002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames[index] = name; 653002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum++; 654002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } else { 655002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (index < mNum) { 656002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mUids, index, mUids, index+1, mNum-index); 657002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn System.arraycopy(mNames, index, mNames, index+1, mNum-index); 658002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 659002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mUids[index] = uid; 660002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNames[index] = name; 661002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn mNum++; 662002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 6637e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 6647e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 6657e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn @Override 6667e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public int describeContents() { 6677e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return 0; 6687e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 6697e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 6707e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn @Override 6717e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public void writeToParcel(Parcel dest, int flags) { 6727e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn dest.writeInt(mNum); 6737e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn dest.writeIntArray(mUids); 674002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn dest.writeStringArray(mNames); 6757e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 6767e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn 6779630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown @Override 6789630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown public String toString() { 6799630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown StringBuilder result = new StringBuilder(); 680002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result.append("WorkSource{"); 6819630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown for (int i = 0; i < mNum; i++) { 6829630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown if (i != 0) { 6839630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown result.append(", "); 6849630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown } 6859630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown result.append(mUids[i]); 686002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn if (mNames != null) { 687002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result.append(" "); 688002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result.append(mNames[i]); 689002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn } 6909630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown } 691002a54e2291eeb3a3fd0b6b3f9dbc96a7c805062Dianne Hackborn result.append("}"); 6929630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown return result.toString(); 6939630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown } 6949630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown 6957e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public static final Parcelable.Creator<WorkSource> CREATOR 6967e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn = new Parcelable.Creator<WorkSource>() { 6977e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource createFromParcel(Parcel in) { 6987e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return new WorkSource(in); 6997e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 7007e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn public WorkSource[] newArray(int size) { 7017e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn return new WorkSource[size]; 7027e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn } 7037e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn }; 7047e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn} 705