| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.internal.util; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.os.Bundle; |
| 21 |
import android.os.Parcelable; |
| 22 |
|
| 23 |
import com.android.internal.os.IResultReceiver; |
| 24 |
|
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.concurrent.CountDownLatch; |
| 27 |
import java.util.concurrent.TimeUnit; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class SyncResultReceiver extends IResultReceiver.Stub { |
| 36 |
|
| 37 |
private static final String EXTRA = "EXTRA"; |
| 38 |
|
| 39 |
private final CountDownLatch mLatch = new CountDownLatch(1); |
| 40 |
private final int mTimeoutMs; |
| 41 |
private int mResult; |
| 42 |
private Bundle mBundle; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public SyncResultReceiver(int timeoutMs) { |
| 50 |
mTimeoutMs = timeoutMs; |
| 51 |
} |
| 52 |
|
| 53 |
private void waitResult() throws TimeoutException { |
| 54 |
try { |
| 55 |
if (!mLatch.await(mTimeoutMs, TimeUnit.MILLISECONDS)) { |
| 56 |
throw new TimeoutException("Not called in " + mTimeoutMs + "ms"); |
| 57 |
} |
| 58 |
} catch (InterruptedException e) { |
| 59 |
Thread.currentThread().interrupt(); |
| 60 |
throw new TimeoutException("Interrupted"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public int getIntResult() throws TimeoutException { |
| 68 |
waitResult(); |
| 69 |
return mResult; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Nullable |
| 76 |
public String getStringResult() throws TimeoutException { |
| 77 |
waitResult(); |
| 78 |
return mBundle == null ? null : mBundle.getString(EXTRA); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Nullable |
| 85 |
public String[] getStringArrayResult() throws TimeoutException { |
| 86 |
waitResult(); |
| 87 |
return mBundle == null ? null : mBundle.getStringArray(EXTRA); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Nullable |
| 94 |
public <P extends Parcelable> P getParcelableResult() throws TimeoutException { |
| 95 |
waitResult(); |
| 96 |
return mBundle == null ? null : mBundle.getParcelable(EXTRA); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Nullable |
| 103 |
public <P extends Parcelable> ArrayList<P> getParcelableListResult() throws TimeoutException { |
| 104 |
waitResult(); |
| 105 |
return mBundle == null ? null : mBundle.getParcelableArrayList(EXTRA); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public int getOptionalExtraIntResult(int defaultValue) throws TimeoutException { |
| 115 |
waitResult(); |
| 116 |
if (mBundle == null || !mBundle.containsKey(EXTRA)) return defaultValue; |
| 117 |
|
| 118 |
return mBundle.getInt(EXTRA); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void send(int resultCode, Bundle resultData) { |
| 123 |
mResult = resultCode; |
| 124 |
mBundle = resultData; |
| 125 |
mLatch.countDown(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@NonNull |
| 133 |
public static Bundle bundleFor(@Nullable String value) { |
| 134 |
final Bundle bundle = new Bundle(); |
| 135 |
bundle.putString(EXTRA, value); |
| 136 |
return bundle; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@NonNull |
| 144 |
public static Bundle bundleFor(@Nullable String[] value) { |
| 145 |
final Bundle bundle = new Bundle(); |
| 146 |
bundle.putStringArray(EXTRA, value); |
| 147 |
return bundle; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
@NonNull |
| 155 |
public static Bundle bundleFor(@Nullable Parcelable value) { |
| 156 |
final Bundle bundle = new Bundle(); |
| 157 |
bundle.putParcelable(EXTRA, value); |
| 158 |
return bundle; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@NonNull |
| 166 |
public static Bundle bundleFor(@Nullable ArrayList<? extends Parcelable> value) { |
| 167 |
final Bundle bundle = new Bundle(); |
| 168 |
bundle.putParcelableArrayList(EXTRA, value); |
| 169 |
return bundle; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
@NonNull |
| 178 |
public static Bundle bundleFor(int value) { |
| 179 |
final Bundle bundle = new Bundle(); |
| 180 |
bundle.putInt(EXTRA, value); |
| 181 |
return bundle; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public static final class TimeoutException extends Exception { |
| 186 |
private TimeoutException(String msg) { |
| 187 |
super(msg); |
| 188 |
} |
| 189 |
} |
| 190 |
} |