20 |
21 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/remoteconfig/demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.demo;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.remoteconfig.demo", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/remoteconfig/library/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.remoteconfig.library.test", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/RemoteError.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | public class RemoteError extends Exception {
4 | public final NetworkResponse networkResponse;
5 | private long networkTimeMs;
6 |
7 | public RemoteError() {
8 | networkResponse = null;
9 | }
10 |
11 | public RemoteError(NetworkResponse response) {
12 | networkResponse = response;
13 | }
14 |
15 | public RemoteError(String exceptionMessage) {
16 | super(exceptionMessage);
17 | networkResponse = null;
18 | }
19 |
20 | public RemoteError(String exceptionMessage, Throwable reason) {
21 | super(exceptionMessage, reason);
22 | networkResponse = null;
23 | }
24 |
25 | public RemoteError(Throwable cause) {
26 | super(cause);
27 | networkResponse = null;
28 | }
29 |
30 | /* package */ void setNetworkTimeMs(long networkTimeMs) {
31 | this.networkTimeMs = networkTimeMs;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 | defaultConfig {
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 |
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | consumerProguardFiles 'consumer-rules.pro'
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | useLibrary 'org.apache.http.legacy'
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 |
29 | implementation 'androidx.appcompat:appcompat:1.1.0'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'androidx.test:runner:1.2.0'
32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
33 |
34 |
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.remoteconfig.demo"
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 |
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'androidx.appcompat:appcompat:1.1.0'
25 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'androidx.test:runner:1.2.0'
28 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
29 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
30 |
31 | implementation project(':library')
32 | }
33 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1024m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
19 | # AndroidX package structure to make it clearer which packages are bundled with the
20 | # Android operating system, and which are packaged with your app's APK
21 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
22 | android.useAndroidX=true
23 | # Automatically convert third-party libraries to use AndroidX
24 | android.enableJetifier=true
25 |
26 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/Network.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | /** An interface for performing requests. */
20 | public interface Network {
21 | /**
22 | * Performs the specified request.
23 | *
24 | * @param request Request to process
25 | * @return A {@link NetworkResponse} with data and caching metadata; will never be null
26 | * @throws RemoteError on errors
27 | */
28 | //performRequest
29 | NetworkResponse performRequest(Request> request) throws RemoteError;
30 | }
31 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/Authenticator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import com.remoteconfig.library.AuthFailureError;
20 |
21 | /** An interface for interacting with auth tokens. */
22 | public interface Authenticator {
23 | /**
24 | * Synchronously retrieves an auth token.
25 | *
26 | * @throws AuthFailureError If authentication did not succeed
27 | */
28 | String getAuthToken() throws AuthFailureError;
29 |
30 | /** Invalidates the provided auth token. */
31 | void invalidateAuthToken(String authToken);
32 | }
33 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/ResponseDelivery.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | public interface ResponseDelivery {
20 | /** Parses a response from the network or cache and delivers it. */
21 | void postResponse(Request> request, Response> response);
22 |
23 | /**
24 | * Parses a response from the network or cache and delivers it. The provided Runnable will be
25 | * executed after delivery.
26 | */
27 | void postResponse(Request> request, Response> response, Runnable runnable);
28 |
29 | /** Posts an error for the given request. */
30 | void postError(Request> request, RemoteError error);
31 | }
32 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/Header.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 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.remoteconfig.library;
17 |
18 | import android.text.TextUtils;
19 |
20 | /** An HTTP header. */
21 |
22 | public final class Header {
23 | private final String mName;
24 | private final String mValue;
25 |
26 | public Header(String name, String value) {
27 | mName = name;
28 | mValue = value;
29 | }
30 |
31 | public final String getName() {
32 | return mName;
33 | }
34 |
35 | public final String getValue() {
36 | return mValue;
37 | }
38 |
39 | @Override
40 | public boolean equals(Object o) {
41 | if (this == o) return true;
42 | if (o == null || getClass() != o.getClass()) return false;
43 |
44 | Header header = (Header) o;
45 |
46 | return TextUtils.equals(mName, header.mName) && TextUtils.equals(mValue, header.mValue);
47 | }
48 |
49 | @Override
50 | public int hashCode() {
51 | int result = mName.hashCode();
52 | result = 31 * result + mValue.hashCode();
53 | return result;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return "Header[name=" + mName + ",value=" + mValue + "]";
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/AuthFailureError.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import android.content.Intent;
20 |
21 | /** Error indicating that there was an authentication failure when performing a Request. */
22 | @SuppressWarnings("serial")
23 | public class AuthFailureError extends RemoteError {
24 | /** An intent that can be used to resolve this exception. (Brings up the password dialog.) */
25 | private Intent mResolutionIntent;
26 |
27 | public AuthFailureError() {}
28 |
29 | public AuthFailureError(Intent intent) {
30 | mResolutionIntent = intent;
31 | }
32 |
33 | public AuthFailureError(NetworkResponse response) {
34 | super(response);
35 | }
36 |
37 | public AuthFailureError(String message) {
38 | super(message);
39 | }
40 |
41 | public AuthFailureError(String message, Exception reason) {
42 | super(message, reason);
43 | }
44 |
45 | public Intent getResolutionIntent() {
46 | return mResolutionIntent;
47 | }
48 |
49 | @Override
50 | public String getMessage() {
51 | if (mResolutionIntent != null) {
52 | return "User needs to (re)enter credentials.";
53 | }
54 | return super.getMessage();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/FetchRemote.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | import android.content.Context;
4 | import com.remoteconfig.library.Network;
5 | import com.remoteconfig.library.RequestQueue;
6 | import com.remoteconfig.library.toolbox.BaseHttpStack;
7 | import com.remoteconfig.library.toolbox.BasicNetwork;
8 | import com.remoteconfig.library.toolbox.DiskBasedCache;
9 | import com.remoteconfig.library.toolbox.HurlStack;
10 |
11 | import java.io.File;
12 |
13 | public class FetchRemote {
14 |
15 | private static final String DEFAULT_CACHE_DIR = "volley";
16 |
17 | // changed public -> private
18 | private static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
19 | BasicNetwork network;
20 | if (stack == null) {
21 | network = new BasicNetwork(new HurlStack());
22 | } else {
23 | network = new BasicNetwork(stack);
24 | }
25 | return newRequestQueue(context, network);
26 | }
27 |
28 | private static RequestQueue newRequestQueue(Context context, Network network) {
29 | final Context appContext = context.getApplicationContext();
30 | DiskBasedCache.FileSupplier cacheSupplier =
31 | new DiskBasedCache.FileSupplier() {
32 | private File cacheDir = null;
33 |
34 | @Override
35 | public File get() {
36 | if (cacheDir == null) {
37 | cacheDir = new File(appContext.getCacheDir(), DEFAULT_CACHE_DIR);
38 | }
39 | return cacheDir;
40 | }
41 | };
42 | RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheSupplier), network);
43 | queue.start();
44 | return queue;
45 | }
46 |
47 | public static RequestQueue newRequestQueue(Context context) {
48 | return newRequestQueue(context, (BaseHttpStack) null);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/HttpStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import com.remoteconfig.library.AuthFailureError;
20 | import com.remoteconfig.library.Request;
21 |
22 | import java.io.IOException;
23 | import java.util.Map;
24 | import org.apache.http.HttpResponse;
25 |
26 | /**
27 | * An HTTP stack abstraction.
28 | *
29 | * @deprecated This interface should be avoided as it depends on the deprecated Apache HTTP library.
30 | * Use {@link BaseHttpStack} to avoid this dependency. This class may be removed in a future
31 | * release of FetchRemote.
32 | */
33 | @SuppressWarnings("DeprecatedIsStillUsed")
34 | @Deprecated
35 | public interface HttpStack {
36 | /**
37 | * Performs an HTTP request with the given parameters.
38 | *
39 | *
A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
40 | * and the Content-Type header is set to request.getPostBodyContentType().
41 | *
42 | * @param request the request to perform
43 | * @param additionalHeaders additional headers to be sent together with {@link
44 | * Request#getHeaders()}
45 | * @return the HTTP response
46 | */
47 | // performRequest
48 | HttpResponse performRequest(Request> request, Map additionalHeaders)
49 | throws IOException, AuthFailureError;
50 | }
51 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/RetryPolicy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | /**
20 | * Retry policy for a request.
21 | *
22 | *
A retry policy can control two parameters:
23 | *
24 | *
25 | *
The number of tries. This can be a simple counter or more complex logic based on the type
26 | * of error passed to {@link #retry(RemoteError)}, although {@link #getCurrentRetryCount()}
27 | * should always return the current retry count for logging purposes.
28 | *
The request timeout for each try, via {@link #getCurrentTimeout()}. In the common case that
29 | * a request times out before the response has been received from the server, retrying again
30 | * with a longer timeout can increase the likelihood of success (at the expense of causing the
31 | * user to wait longer, especially if the request still fails).
32 | *
33 | *
34 | *
Note that currently, retries triggered by a retry policy are attempted immediately in sequence
35 | * with no delay between them (although the time between tries may increase if the requests are
36 | * timing out and {@link #getCurrentTimeout()} is returning increasing values).
37 | *
38 | *
By default, FetchRemote uses {@link DefaultRetryPolicy}.
39 | */
40 | public interface RetryPolicy {
41 |
42 | /** Returns the current timeout (used for logging). */
43 | int getCurrentTimeout();
44 |
45 | /** Returns the current retry count (used for logging). */
46 | int getCurrentRetryCount();
47 |
48 | /**
49 | * Prepares for the next retry by applying a backoff to the timeout.
50 | *
51 | * @param error The error code of the last attempt.
52 | * @throws RemoteError In the event that the retry could not be performed (for example if we ran
53 | * out of attempts), the passed in error is thrown.
54 | */
55 | void retry(RemoteError error) throws RemoteError;
56 | }
57 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/RemoteConfig.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import androidx.annotation.GuardedBy;
7 | import androidx.annotation.Nullable;
8 | import com.remoteconfig.library.NetworkResponse;
9 | import com.remoteconfig.library.RemoteParams;
10 | import com.remoteconfig.library.Request;
11 | import com.remoteconfig.library.Response;
12 | import com.remoteconfig.library.Response.ErrorListener;
13 | import com.remoteconfig.library.Response.Listener;
14 | import com.remoteconfig.library.toolbox.HttpHeaderParser;
15 |
16 | import java.io.UnsupportedEncodingException;
17 |
18 | /** A canned request for retrieving the response body at a given URL as a String. */
19 |
20 | public class RemoteConfig extends Request {
21 |
22 | private final Object mLock = new Object();
23 |
24 | private Context mContext;
25 |
26 | @Nullable
27 | @GuardedBy("mLock")
28 | private Listener mListener;
29 |
30 | /**
31 | * Creates a new request with the given method.
32 | *
33 | */
34 |
35 | public RemoteConfig(
36 | Context context,
37 | String url,
38 | @Nullable Listener listener,
39 | @Nullable ErrorListener errorListener) {
40 | super(0, url, errorListener);
41 | mListener = listener;
42 | mContext = context;
43 | }
44 |
45 |
46 | @Override
47 | public void cancel() {
48 | super.cancel();
49 | synchronized (mLock) {
50 | mListener = null;
51 | }
52 | }
53 |
54 | @Override
55 | protected void deliverResponse(String response) {
56 | Listener listener;
57 | synchronized (mLock) {
58 | listener = mListener;
59 | }
60 | if (listener != null) {
61 | // set values
62 | Log.e("onComplete", response);
63 | RemoteParams remoteParams = new RemoteParams(mContext);
64 | remoteParams.setResponse(response);
65 | listener.onComplete();
66 | }
67 |
68 | }
69 |
70 | @Override
71 | @SuppressWarnings("DefaultCharset")
72 | protected Response parseNetworkResponse(NetworkResponse response) {
73 | String parsed;
74 | try {
75 | parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
76 | } catch (UnsupportedEncodingException e) {
77 | parsed = new String(response.data);
78 | }
79 | return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/Response.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | /**
20 | * Encapsulates a parsed response for delivery.
21 | *
22 | * @param Parsed type of this response
23 | */
24 | public class Response {
25 |
26 | /** Callback interface for delivering parsed responses. */
27 | public interface Listener {
28 | /** Called when a response is received. */
29 | void onComplete();
30 | }
31 |
32 | /** Callback interface for delivering error responses. */
33 | public interface ErrorListener {
34 | /**
35 | * Callback method that an error has been occurred with the provided error code and optional
36 | * user-readable message.
37 | */
38 | void onError(RemoteError error);
39 | }
40 |
41 | /** Returns a successful response containing the parsed result. */
42 | public static Response success(T result, Cache.Entry cacheEntry) {
43 | return new Response<>(result, cacheEntry);
44 | }
45 |
46 | /**
47 | * Returns a failed response containing the given error code and an optional localized message
48 | * displayed to the user.
49 | */
50 | public static Response error(RemoteError error) {
51 | return new Response<>(error);
52 | }
53 |
54 | /** Parsed response, or null in the case of error. */
55 | public final T result;
56 |
57 | /** Cache metadata for this response, or null in the case of error. */
58 | public final Cache.Entry cacheEntry;
59 |
60 | /** Detailed error information if errorCode != OK. */
61 | public final RemoteError error;
62 |
63 | /** True if this response was a soft-expired one and a second one MAY be coming. */
64 | public boolean intermediate = false;
65 |
66 | /** Returns whether this response is considered successful. */
67 | public boolean isSuccess() {
68 | return error == null;
69 | }
70 |
71 | private Response(T result, Cache.Entry cacheEntry) {
72 | this.result = result;
73 | this.cacheEntry = cacheEntry;
74 | this.error = null;
75 | }
76 |
77 | private Response(RemoteError error) {
78 | this.result = null;
79 | this.cacheEntry = null;
80 | this.error = error;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/HttpResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 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.remoteconfig.library.toolbox;
17 |
18 | import com.remoteconfig.library.Header;
19 |
20 | import java.io.InputStream;
21 | import java.util.Collections;
22 | import java.util.List;
23 |
24 | /** A response from an HTTP server. */
25 | public final class HttpResponse {
26 |
27 | private final int mStatusCode;
28 | private final List mHeaders;
29 | private final int mContentLength;
30 | private final InputStream mContent;
31 |
32 | /**
33 | * Construct a new HttpResponse for an empty response body.
34 | *
35 | * @param statusCode the HTTP status code of the response
36 | * @param headers the response headers
37 | */
38 | public HttpResponse(int statusCode, List headers) {
39 | this(statusCode, headers, /* contentLength= */ -1, /* content= */ null);
40 | }
41 |
42 | /**
43 | * Construct a new HttpResponse.
44 | *
45 | * @param statusCode the HTTP status code of the response
46 | * @param headers the response headers
47 | * @param contentLength the length of the response content. Ignored if there is no content.
48 | * @param content an {@link InputStream} of the response content. May be null to indicate that
49 | * the response has no content.
50 | */
51 | public HttpResponse(
52 | int statusCode, List headers, int contentLength, InputStream content) {
53 | mStatusCode = statusCode;
54 | mHeaders = headers;
55 | mContentLength = contentLength;
56 | mContent = content;
57 | }
58 |
59 | /** Returns the HTTP status code of the response. */
60 | public final int getStatusCode() {
61 | return mStatusCode;
62 | }
63 |
64 | /** Returns the response headers. Must not be mutated directly. */
65 | public final List getHeaders() {
66 | return Collections.unmodifiableList(mHeaders);
67 | }
68 |
69 | /** Returns the length of the content. Only valid if {@link #getContent} is non-null. */
70 | public final int getContentLength() {
71 | return mContentLength;
72 | }
73 |
74 | /**
75 | * Returns an {@link InputStream} of the response content. May be null to indicate that the
76 | * response has no content.
77 | */
78 | public final InputStream getContent() {
79 | return mContent;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/AdaptedHttpStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 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.remoteconfig.library.toolbox;
17 |
18 | import com.remoteconfig.library.AuthFailureError;
19 | import com.remoteconfig.library.Header;
20 | import com.remoteconfig.library.Request;
21 |
22 | import java.io.IOException;
23 | import java.net.SocketTimeoutException;
24 | import java.util.ArrayList;
25 | import java.util.List;
26 | import java.util.Map;
27 | import org.apache.http.conn.ConnectTimeoutException;
28 |
29 | /**
30 | * {@link BaseHttpStack} implementation wrapping a {@link HttpStack}.
31 | *
32 | *
{@link BasicNetwork} uses this if it is provided a {@link HttpStack} at construction time,
33 | * allowing it to have one implementation based atop {@link BaseHttpStack}.
34 | */
35 | @SuppressWarnings("deprecation")
36 | class AdaptedHttpStack extends BaseHttpStack {
37 |
38 | private final HttpStack mHttpStack;
39 |
40 | AdaptedHttpStack(HttpStack httpStack) {
41 | mHttpStack = httpStack;
42 | }
43 |
44 | @Override
45 | public HttpResponse executeRequest(Request> request, Map additionalHeaders)
46 | throws IOException, AuthFailureError {
47 | org.apache.http.HttpResponse apacheResp;
48 | try {
49 | //performRequest
50 | apacheResp = mHttpStack.performRequest(request, additionalHeaders);
51 | } catch (ConnectTimeoutException e) {
52 | // BasicNetwork won't know that this exception should be retried like a timeout, since
53 | // it's an Apache-specific error, so wrap it in a standard timeout exception.
54 | throw new SocketTimeoutException(e.getMessage());
55 | }
56 |
57 | int statusCode = apacheResp.getStatusLine().getStatusCode();
58 |
59 | org.apache.http.Header[] headers = apacheResp.getAllHeaders();
60 | List headerList = new ArrayList<>(headers.length);
61 | for (org.apache.http.Header header : headers) {
62 | headerList.add(new Header(header.getName(), header.getValue()));
63 | }
64 |
65 | if (apacheResp.getEntity() == null) {
66 | return new HttpResponse(statusCode, headerList);
67 | }
68 |
69 | long contentLength = apacheResp.getEntity().getContentLength();
70 | if ((int) contentLength != contentLength) {
71 | throw new IOException("Response too large: " + contentLength);
72 | }
73 |
74 | return new HttpResponse(
75 | statusCode,
76 | headerList,
77 | (int) apacheResp.getEntity().getContentLength(),
78 | apacheResp.getEntity().getContent());
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/PoolingByteArrayOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 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.remoteconfig.library.toolbox;
18 |
19 | import java.io.ByteArrayOutputStream;
20 | import java.io.IOException;
21 |
22 | /**
23 | * A variation of {@link ByteArrayOutputStream} that uses a pool of byte[] buffers instead
24 | * of always allocating them fresh, saving on heap churn.
25 | */
26 | public class PoolingByteArrayOutputStream extends ByteArrayOutputStream {
27 | /**
28 | * If the {@link #PoolingByteArrayOutputStream(ByteArrayPool)} constructor is called, this is
29 | * the default size to which the underlying byte array is initialized.
30 | */
31 | private static final int DEFAULT_SIZE = 256;
32 |
33 | private final ByteArrayPool mPool;
34 |
35 | /**
36 | * Constructs a new PoolingByteArrayOutputStream with a default size. If more bytes are written
37 | * to this instance, the underlying byte array will expand.
38 | */
39 | public PoolingByteArrayOutputStream(ByteArrayPool pool) {
40 | this(pool, DEFAULT_SIZE);
41 | }
42 |
43 | /**
44 | * Constructs a new {@code ByteArrayOutputStream} with a default size of {@code size} bytes. If
45 | * more than {@code size} bytes are written to this instance, the underlying byte array will
46 | * expand.
47 | *
48 | * @param size initial size for the underlying byte array. The value will be pinned to a default
49 | * minimum size.
50 | */
51 | public PoolingByteArrayOutputStream(ByteArrayPool pool, int size) {
52 | mPool = pool;
53 | buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
54 | }
55 |
56 | @Override
57 | public void close() throws IOException {
58 | mPool.returnBuf(buf);
59 | buf = null;
60 | super.close();
61 | }
62 |
63 | @Override
64 | public void finalize() {
65 | mPool.returnBuf(buf);
66 | }
67 |
68 | /** Ensures there is enough space in the buffer for the given number of additional bytes. */
69 | @SuppressWarnings("UnsafeFinalization")
70 | private void expand(int i) {
71 | /* Can the buffer handle @i more bytes, if not expand it */
72 | if (count + i <= buf.length) {
73 | return;
74 | }
75 | byte[] newbuf = mPool.getBuf((count + i) * 2);
76 | System.arraycopy(buf, 0, newbuf, 0, count);
77 | mPool.returnBuf(buf);
78 | buf = newbuf;
79 | }
80 |
81 | @Override
82 | public synchronized void write(byte[] buffer, int offset, int len) {
83 | expand(len);
84 | super.write(buffer, offset, len);
85 | }
86 |
87 | @Override
88 | public synchronized void write(int oneByte) {
89 | expand(1);
90 | super.write(oneByte);
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/DefaultRetryPolicy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | /** Default retry policy for requests. */
20 | public class DefaultRetryPolicy implements RetryPolicy {
21 | /** The current timeout in milliseconds. */
22 | private int mCurrentTimeoutMs;
23 |
24 | /** The current retry count. */
25 | private int mCurrentRetryCount;
26 |
27 | /** The maximum number of attempts. */
28 | private final int mMaxNumRetries;
29 |
30 | /** The backoff multiplier for the policy. */
31 | private final float mBackoffMultiplier;
32 |
33 | /** The default socket timeout in milliseconds */
34 | public static final int DEFAULT_TIMEOUT_MS = 2500;
35 |
36 | /** The default number of retries */
37 | public static final int DEFAULT_MAX_RETRIES = 1;
38 |
39 | /** The default backoff multiplier */
40 | public static final float DEFAULT_BACKOFF_MULT = 1f;
41 |
42 | /** Constructs a new retry policy using the default timeouts. */
43 | public DefaultRetryPolicy() {
44 | this(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
45 | }
46 |
47 | /**
48 | * Constructs a new retry policy.
49 | *
50 | * @param initialTimeoutMs The initial timeout for the policy.
51 | * @param maxNumRetries The maximum number of retries.
52 | * @param backoffMultiplier Backoff multiplier for the policy.
53 | */
54 | public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoffMultiplier) {
55 | mCurrentTimeoutMs = initialTimeoutMs;
56 | mMaxNumRetries = maxNumRetries;
57 | mBackoffMultiplier = backoffMultiplier;
58 | }
59 |
60 | /** Returns the current timeout. */
61 | @Override
62 | public int getCurrentTimeout() {
63 | return mCurrentTimeoutMs;
64 | }
65 |
66 | /** Returns the current retry count. */
67 | @Override
68 | public int getCurrentRetryCount() {
69 | return mCurrentRetryCount;
70 | }
71 |
72 | /** Returns the backoff multiplier for the policy. */
73 | public float getBackoffMultiplier() {
74 | return mBackoffMultiplier;
75 | }
76 |
77 | /**
78 | * Prepares for the next retry by applying a backoff to the timeout.
79 | *
80 | * @param error The error code of the last attempt.
81 | */
82 | @Override
83 | public void retry(RemoteError error) throws RemoteError {
84 | mCurrentRetryCount++;
85 | mCurrentTimeoutMs += (int) (mCurrentTimeoutMs * mBackoffMultiplier);
86 | if (!hasAttemptRemaining()) {
87 | throw error;
88 | }
89 | }
90 |
91 | /** Returns true if this policy has attempts remaining, false otherwise. */
92 | protected boolean hasAttemptRemaining() {
93 | return mCurrentRetryCount <= mMaxNumRetries;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/remoteconfig/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.demo;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 |
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.TextView;
9 |
10 |
11 | import com.remoteconfig.library.*;
12 |
13 | import org.json.JSONArray;
14 | import org.json.JSONObject;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 |
19 | TextView textViewSimpleText, textViewNumber, textViewJSONObject,textViewJSONArray, textViewBoolean;
20 | Button buttonRequest;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 |
27 | textViewSimpleText = findViewById(R.id.textViewSimpleText);
28 | textViewNumber = findViewById(R.id.textViewNumber);
29 | textViewJSONObject = findViewById(R.id.textViewJSONObject);
30 | textViewJSONArray = findViewById(R.id.textViewJSONArray);
31 | textViewBoolean = findViewById(R.id.textViewBoolean);
32 |
33 | buttonRequest = findViewById(R.id.buttonRequest);
34 |
35 |
36 | buttonRequest.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View view) {
39 |
40 | // set request
41 | RequestQueue queue = FetchRemote.newRequestQueue(MainActivity.this);
42 |
43 | // url of the json file
44 | String mUrl ="https://raw.githubusercontent.com/gayankuruppu/android-remote-config-library/master/remote-config.json";
45 |
46 | // request the json file
47 | RemoteConfig remoteConfig = new RemoteConfig(MainActivity.this, mUrl,
48 | new Response.Listener() {
49 | @Override
50 | public void onComplete() {
51 | // json file retrieved
52 | RemoteParams remoteParams = new RemoteParams(MainActivity.this);
53 | textViewSimpleText.setText(remoteParams.getString("short_text", "default_text"));
54 |
55 | int intValue = remoteParams.getInt("number", 200);
56 | textViewNumber.setText(String.valueOf(intValue));
57 |
58 | JSONObject jsonObject = remoteParams.getJSONObject("json_object");
59 | textViewJSONObject.setText(String.valueOf(jsonObject));
60 |
61 | JSONArray jsonArray = remoteParams.getJSONArray("json_array");
62 | textViewJSONArray.setText(String.valueOf(jsonArray));
63 |
64 | boolean booleanValue = remoteParams.getBoolean("boolean", false);
65 | textViewBoolean.setText(String.valueOf(booleanValue));
66 |
67 | }
68 | },
69 | new Response.ErrorListener() {
70 | @Override
71 | public void onError(RemoteError error) {
72 | // json file retrieve error
73 |
74 | }
75 | }
76 | );
77 |
78 | // clear cache
79 | remoteConfig.setShouldCache(false);
80 | queue.add(remoteConfig);
81 |
82 | }
83 | });
84 |
85 |
86 |
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/Cache.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import java.util.Collections;
20 | import java.util.List;
21 | import java.util.Map;
22 |
23 | /** An interface for a cache keyed by a String with a byte array as data. */
24 | public interface Cache {
25 | /**
26 | * Retrieves an entry from the cache.
27 | *
28 | * @param key Cache key
29 | * @return An {@link Entry} or null in the event of a cache miss
30 | */
31 | Entry get(String key);
32 |
33 | /**
34 | * Adds or replaces an entry to the cache.
35 | *
36 | * @param key Cache key
37 | * @param entry Data to store and metadata for cache coherency, TTL, etc.
38 | */
39 | void put(String key, Entry entry);
40 |
41 | /**
42 | * Performs any potentially long-running actions needed to initialize the cache; will be called
43 | * from a worker thread.
44 | */
45 | void initialize();
46 |
47 | /**
48 | * Invalidates an entry in the cache.
49 | *
50 | * @param key Cache key
51 | * @param fullExpire True to fully expire the entry, false to soft expire
52 | */
53 | void invalidate(String key, boolean fullExpire);
54 |
55 | /**
56 | * Removes an entry from the cache.
57 | *
58 | * @param key Cache key
59 | */
60 | void remove(String key);
61 |
62 | /** Empties the cache. */
63 | void clear();
64 |
65 | /** Data and metadata for an entry returned by the cache. */
66 | class Entry {
67 | /** The data returned from cache. */
68 | public byte[] data;
69 |
70 | /** ETag for cache coherency. */
71 | public String etag;
72 |
73 | /** Date of this response as reported by the server. */
74 | public long serverDate;
75 |
76 | /** The last modified date for the requested object. */
77 | public long lastModified;
78 |
79 | /** TTL for this record. */
80 | public long ttl;
81 |
82 | /** Soft TTL for this record. */
83 | public long softTtl;
84 |
85 | /**
86 | * Response headers as received from server; must be non-null. Should not be mutated
87 | * directly.
88 | *
89 | *
Note that if the server returns two headers with the same (case-insensitive) name,
90 | * this map will only contain the one of them. {@link #allResponseHeaders} may contain all
91 | * headers if the {@link Cache} implementation supports it.
92 | */
93 | public Map responseHeaders = Collections.emptyMap();
94 |
95 | /**
96 | * All response headers. May be null depending on the {@link Cache} implementation. Should
97 | * not be mutated directly.
98 | */
99 | public List allResponseHeaders;
100 |
101 | /** True if the entry is expired. */
102 | public boolean isExpired() {
103 | return this.ttl < System.currentTimeMillis();
104 | }
105 |
106 | /** True if a refresh is needed from the original data source. */
107 | public boolean refreshNeeded() {
108 | return this.softTtl < System.currentTimeMillis();
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/RemoteParams.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.preference.PreferenceManager;
6 |
7 | import org.json.JSONArray;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 | public class RemoteParams {
12 |
13 | private Context context;
14 | String PREF = "remote_params";
15 | String DEFAULT_VALUE = "defaultValue";
16 |
17 | public RemoteParams(Context context){
18 | this.context = context;
19 | }
20 |
21 | public void setResponse(String response){
22 |
23 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
24 | SharedPreferences.Editor editor = prefs.edit();
25 | editor.putString(PREF, response);
26 | editor.apply();
27 | }
28 |
29 | public String getString(String param, String defaultValue){
30 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
31 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
32 | try {
33 | JSONObject jsonResponse = new JSONObject(jsonPref);
34 | return jsonResponse.getString(param);
35 | } catch (JSONException e) {
36 | return defaultValue;
37 | }
38 | }
39 |
40 | public int getInt(String param, int defaultValue){
41 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
42 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
43 | try {
44 | JSONObject jsonResponse = new JSONObject(jsonPref);
45 | return jsonResponse.getInt(param);
46 | } catch (JSONException e) {
47 | return defaultValue;
48 | }
49 | }
50 |
51 | public JSONObject getJSONObject(String param){
52 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
53 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
54 | try {
55 | JSONObject jsonResponse = new JSONObject(jsonPref);
56 | return jsonResponse.getJSONObject(param);
57 | } catch (JSONException e) {
58 | return null;
59 | }
60 | }
61 |
62 | public JSONObject getJSONObject(String param, JSONObject jsonObject){
63 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
64 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
65 | try {
66 | JSONObject jsonResponse = new JSONObject(jsonPref);
67 | return jsonResponse.getJSONObject(param);
68 | } catch (JSONException e) {
69 | return jsonObject;
70 | }
71 | }
72 |
73 | public JSONArray getJSONArray(String param){
74 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
75 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
76 | try {
77 | JSONObject jsonResponse = new JSONObject(jsonPref);
78 | return jsonResponse.getJSONArray(param);
79 | } catch (JSONException e) {
80 | return null;
81 | }
82 | }
83 |
84 | public JSONArray getJSONArray(String param, JSONArray jsonArray){
85 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
86 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
87 | try {
88 | JSONObject jsonResponse = new JSONObject(jsonPref);
89 | return jsonResponse.getJSONArray(param);
90 | } catch (JSONException e) {
91 | return jsonArray;
92 | }
93 | }
94 |
95 | public boolean getBoolean(String param, boolean defaultValue){
96 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
97 | String jsonPref = prefs.getString(PREF, DEFAULT_VALUE);
98 | try {
99 | JSONObject jsonResponse = new JSONObject(jsonPref);
100 | return jsonResponse.getBoolean(param);
101 | } catch (JSONException e) {
102 | return defaultValue;
103 | }
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/BaseHttpStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 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.remoteconfig.library.toolbox;
17 |
18 | import com.remoteconfig.library.AuthFailureError;
19 | import com.remoteconfig.library.Header;
20 | import com.remoteconfig.library.Request;
21 |
22 | import java.io.IOException;
23 | import java.io.InputStream;
24 | import java.net.SocketTimeoutException;
25 | import java.util.ArrayList;
26 | import java.util.List;
27 | import java.util.Map;
28 | import org.apache.http.ProtocolVersion;
29 | import org.apache.http.StatusLine;
30 | import org.apache.http.entity.BasicHttpEntity;
31 | import org.apache.http.message.BasicHeader;
32 | import org.apache.http.message.BasicHttpResponse;
33 | import org.apache.http.message.BasicStatusLine;
34 |
35 | /** An HTTP stack abstraction. */
36 | @SuppressWarnings("deprecation") // for HttpStack
37 | public abstract class BaseHttpStack implements HttpStack {
38 |
39 | /**
40 | * Performs an HTTP request with the given parameters.
41 | *
42 | *
A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
43 | * and the Content-Type header is set to request.getPostBodyContentType().
44 | *
45 | * @param request the request to perform
46 | * @param additionalHeaders additional headers to be sent together with {@link
47 | * Request#getHeaders()}
48 | * @return the {@link HttpResponse}
49 | * @throws SocketTimeoutException if the request times out
50 | * @throws IOException if another I/O error occurs during the request
51 | * @throws AuthFailureError if an authentication failure occurs during the request
52 | */
53 | public abstract HttpResponse executeRequest(
54 | Request> request, Map additionalHeaders)
55 | throws IOException, AuthFailureError;
56 |
57 | /**
58 | * @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated
59 | * Apache HTTP library. Nothing in FetchRemote's own source calls this method. However, since
60 | * {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation
61 | * in case legacy client apps are dependent on that field. This method may be removed in a
62 | * future release of FetchRemote.
63 | */
64 | @Deprecated
65 | @Override
66 | public final org.apache.http.HttpResponse performRequest(
67 | Request> request, Map additionalHeaders)
68 | throws IOException, AuthFailureError {
69 | HttpResponse response = executeRequest(request, additionalHeaders);
70 |
71 | ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
72 | StatusLine statusLine =
73 | new BasicStatusLine(
74 | protocolVersion, response.getStatusCode(), /* reasonPhrase= */ "");
75 | BasicHttpResponse apacheResponse = new BasicHttpResponse(statusLine);
76 |
77 | List headers = new ArrayList<>();
78 | for (Header header : response.getHeaders()) {
79 | headers.add(new BasicHeader(header.getName(), header.getValue()));
80 | }
81 | apacheResponse.setHeaders(headers.toArray(new org.apache.http.Header[0]));
82 |
83 | InputStream responseStream = response.getContent();
84 | if (responseStream != null) {
85 | BasicHttpEntity entity = new BasicHttpEntity();
86 | entity.setContent(responseStream);
87 | entity.setContentLength(response.getContentLength());
88 | apacheResponse.setEntity(entity);
89 | }
90 |
91 | return apacheResponse;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/AndroidAuthenticator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import android.accounts.Account;
20 | import android.accounts.AccountManager;
21 | import android.accounts.AccountManagerFuture;
22 | import android.annotation.SuppressLint;
23 | import android.content.Context;
24 | import android.content.Intent;
25 | import android.os.Bundle;
26 | import androidx.annotation.VisibleForTesting;
27 | import com.remoteconfig.library.AuthFailureError;
28 |
29 | /**
30 | * An Authenticator that uses {@link AccountManager} to get auth tokens of a specified type for a
31 | * specified account.
32 | */
33 | // TODO: Update this to account for runtime permissions
34 | @SuppressLint("MissingPermission")
35 | public class AndroidAuthenticator implements Authenticator {
36 | private final AccountManager mAccountManager;
37 | private final Account mAccount;
38 | private final String mAuthTokenType;
39 | private final boolean mNotifyAuthFailure;
40 |
41 | /**
42 | * Creates a new authenticator.
43 | *
44 | * @param context Context for accessing AccountManager
45 | * @param account Account to authenticate as
46 | * @param authTokenType Auth token type passed to AccountManager
47 | */
48 | public AndroidAuthenticator(Context context, Account account, String authTokenType) {
49 | this(context, account, authTokenType, /* notifyAuthFailure= */ false);
50 | }
51 |
52 | /**
53 | * Creates a new authenticator.
54 | *
55 | * @param context Context for accessing AccountManager
56 | * @param account Account to authenticate as
57 | * @param authTokenType Auth token type passed to AccountManager
58 | * @param notifyAuthFailure Whether to raise a notification upon auth failure
59 | */
60 | public AndroidAuthenticator(
61 | Context context, Account account, String authTokenType, boolean notifyAuthFailure) {
62 | this(AccountManager.get(context), account, authTokenType, notifyAuthFailure);
63 | }
64 |
65 | @VisibleForTesting
66 | AndroidAuthenticator(
67 | AccountManager accountManager,
68 | Account account,
69 | String authTokenType,
70 | boolean notifyAuthFailure) {
71 | mAccountManager = accountManager;
72 | mAccount = account;
73 | mAuthTokenType = authTokenType;
74 | mNotifyAuthFailure = notifyAuthFailure;
75 | }
76 |
77 | /** Returns the Account being used by this authenticator. */
78 | public Account getAccount() {
79 | return mAccount;
80 | }
81 |
82 | /** Returns the Auth Token Type used by this authenticator. */
83 | public String getAuthTokenType() {
84 | return mAuthTokenType;
85 | }
86 |
87 | // TODO: Figure out what to do about notifyAuthFailure
88 | @SuppressWarnings("deprecation")
89 | @Override
90 | public String getAuthToken() throws AuthFailureError {
91 | AccountManagerFuture future =
92 | mAccountManager.getAuthToken(
93 | mAccount,
94 | mAuthTokenType,
95 | mNotifyAuthFailure,
96 | /* callback= */ null,
97 | /* handler= */ null);
98 | Bundle result;
99 | try {
100 | result = future.getResult();
101 | } catch (Exception e) {
102 | throw new AuthFailureError("Error while retrieving auth token", e);
103 | }
104 | String authToken = null;
105 | if (future.isDone() && !future.isCancelled()) {
106 | if (result.containsKey(AccountManager.KEY_INTENT)) {
107 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
108 | throw new AuthFailureError(intent);
109 | }
110 | authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
111 | }
112 | if (authToken == null) {
113 | throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
114 | }
115 |
116 | return authToken;
117 | }
118 |
119 | @Override
120 | public void invalidateAuthToken(String authToken) {
121 | mAccountManager.invalidateAuthToken(mAccount.type, authToken);
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/ExecutorDelivery.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import android.os.Handler;
20 |
21 | import java.util.concurrent.Executor;
22 |
23 | /** Delivers responses and errors. */
24 | public class ExecutorDelivery implements ResponseDelivery {
25 | /** Used for posting responses, typically to the main thread. */
26 | private final Executor mResponsePoster;
27 |
28 | /**
29 | * Creates a new response delivery interface.
30 | *
31 | * @param handler {@link Handler} to post responses on
32 | */
33 | public ExecutorDelivery(final Handler handler) {
34 | // Make an Executor that just wraps the handler.
35 | mResponsePoster =
36 | new Executor() {
37 | @Override
38 | public void execute(Runnable command) {
39 | handler.post(command);
40 | }
41 | };
42 | }
43 |
44 | /**
45 | * Creates a new response delivery interface, mockable version for testing.
46 | *
47 | * @param executor For running delivery tasks
48 | */
49 | public ExecutorDelivery(Executor executor) {
50 | mResponsePoster = executor;
51 | }
52 |
53 | @Override
54 | public void postResponse(Request> request, Response> response) {
55 | postResponse(request, response, null);
56 | }
57 |
58 | @Override
59 | public void postResponse(Request> request, Response> response, Runnable runnable) {
60 | request.markDelivered();
61 | request.addMarker("post-response");
62 | mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
63 | }
64 |
65 | @Override
66 | public void postError(Request> request, RemoteError error) {
67 | request.addMarker("post-error");
68 | Response> response = Response.error(error);
69 | mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, null));
70 | }
71 |
72 | /** A Runnable used for delivering network responses to a listener on the main thread. */
73 | @SuppressWarnings("rawtypes")
74 | private static class ResponseDeliveryRunnable implements Runnable {
75 | private final Request mRequest;
76 | private final Response mResponse;
77 | private final Runnable mRunnable;
78 |
79 | public ResponseDeliveryRunnable(Request request, Response response, Runnable runnable) {
80 | mRequest = request;
81 | mResponse = response;
82 | mRunnable = runnable;
83 | }
84 |
85 | @SuppressWarnings("unchecked")
86 | @Override
87 | public void run() {
88 | // NOTE: If cancel() is called off the thread that we're currently running in (by
89 | // default, the main thread), we cannot guarantee that deliverResponse()/deliverError()
90 | // won't be called, since it may be canceled after we check isCanceled() but before we
91 | // deliver the response. Apps concerned about this guarantee must either call cancel()
92 | // from the same thread or implement their own guarantee about not invoking their
93 | // listener after cancel() has been called.
94 |
95 | // If this request has canceled, finish it and don't deliver.
96 | if (mRequest.isCanceled()) {
97 | mRequest.finish("canceled-at-delivery");
98 | return;
99 | }
100 |
101 | // Deliver a normal response or error, depending.
102 | if (mResponse.isSuccess()) {
103 | mRequest.deliverResponse(mResponse.result);
104 | } else {
105 | mRequest.deliverError(mResponse.error);
106 | }
107 |
108 | // If this is an intermediate response, add a marker, otherwise we're done
109 | // and the request can be finished.
110 | if (mResponse.intermediate) {
111 | mRequest.addMarker("intermediate-response");
112 | } else {
113 | mRequest.finish("done");
114 | }
115 |
116 | // If we have been provided a post-delivery runnable, run it.
117 | if (mRunnable != null) {
118 | mRunnable.run();
119 | }
120 | }
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
19 |
20 |
25 |
26 |
29 |
30 |
36 |
37 |
38 |
43 |
44 |
49 |
50 |
53 |
54 |
60 |
61 |
62 |
63 |
68 |
69 |
74 |
75 |
76 |
79 |
80 |
86 |
87 |
88 |
93 |
94 |
99 |
100 |
101 |
104 |
105 |
111 |
112 |
113 |
118 |
119 |
124 |
125 |
126 |
129 |
130 |
136 |
137 |
138 |
143 |
144 |
145 |
146 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/VolleyLog.java:
--------------------------------------------------------------------------------
1 | package com.remoteconfig.library;
2 |
3 | import android.os.SystemClock;
4 | import android.util.Log;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.Locale;
8 |
9 | /**
10 | * Logging helper class.
11 | */
12 |
13 | public class VolleyLog {
14 | private static String TAG = "RemoteConfig";
15 |
16 | public static boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
17 |
18 | private static final String CLASS_NAME = VolleyLog.class.getName();
19 |
20 | public static void v(String format, Object... args) {
21 | if (DEBUG) {
22 | Log.v(TAG, buildMessage(format, args));
23 | }
24 | }
25 |
26 | public static void d(String format, Object... args) {
27 | Log.d(TAG, buildMessage(format, args));
28 | }
29 |
30 | public static void e(String format, Object... args) {
31 | Log.e(TAG, buildMessage(format, args));
32 | }
33 |
34 | public static void e(Throwable tr, String format, Object... args) {
35 | Log.e(TAG, buildMessage(format, args), tr);
36 | }
37 |
38 | /**
39 | * Formats the caller's provided message and prepends useful info like calling thread ID and
40 | * method name.
41 | */
42 |
43 | private static String buildMessage(String format, Object... args) {
44 | String msg = (args == null) ? format : String.format(Locale.US, format, args);
45 | StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
46 |
47 | String caller = "";
48 | // Walk up the stack looking for the first caller outside of VolleyLog.
49 | // It will be at least two frames up, so start there.
50 | for (int i = 2; i < trace.length; i++) {
51 | String clazz = trace[i].getClassName();
52 | if (!clazz.equals(VolleyLog.CLASS_NAME)) {
53 | String callingClass = trace[i].getClassName();
54 | callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
55 | callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
56 |
57 | caller = callingClass + "." + trace[i].getMethodName();
58 | break;
59 | }
60 | }
61 | return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller, msg);
62 | }
63 |
64 | /** A simple event log with records containing a name, thread ID, and timestamp. */
65 | static class MarkerLog {
66 | static final boolean ENABLED = VolleyLog.DEBUG;
67 |
68 | /** Minimum duration from first marker to last in an marker log to warrant logging. */
69 | private static final long MIN_DURATION_FOR_LOGGING_MS = 0;
70 |
71 | private static class Marker {
72 | public final String name;
73 | private final long thread;
74 | private final long time;
75 |
76 | private Marker(String name, long thread, long time) {
77 | this.name = name;
78 | this.thread = thread;
79 | this.time = time;
80 | }
81 | }
82 |
83 | private final List mMarkers = new ArrayList<>();
84 | private boolean mFinished = false;
85 |
86 | /** Adds a marker to this log with the specified name. */
87 | public synchronized void add(String name, long threadId) {
88 | if (mFinished) {
89 | throw new IllegalStateException("Marker added to finished log");
90 | }
91 |
92 | mMarkers.add(new Marker(name, threadId, SystemClock.elapsedRealtime()));
93 | }
94 |
95 | /**
96 | * Closes the log, dumping it to logcat if the time difference between the first and last
97 | * markers is greater than {@link #MIN_DURATION_FOR_LOGGING_MS}.
98 | *
99 | * @param header Header string to print above the marker log.
100 | */
101 |
102 | synchronized void finish(String header) {
103 | mFinished = true;
104 |
105 | long duration = getTotalDuration();
106 | if (duration <= MIN_DURATION_FOR_LOGGING_MS) {
107 | return;
108 | }
109 |
110 | long prevTime = mMarkers.get(0).time;
111 | d("(%-4d ms) %s", duration, header);
112 | for (Marker marker : mMarkers) {
113 | long thisTime = marker.time;
114 | d("(+%-4d) [%2d] %s", (thisTime - prevTime), marker.thread, marker.name);
115 | prevTime = thisTime;
116 | }
117 | }
118 |
119 | @Override
120 | protected void finalize() {
121 | // Catch requests that have been collected (and hence end-of-lifted)
122 | // but had no debugging output printed for them.
123 | if (!mFinished) {
124 | finish("Request on the loose");
125 | e("Marker log finalized without finish() - uncaught exit point for request");
126 | }
127 | }
128 |
129 | /** Returns the time difference between the first and last events in this log. */
130 | private long getTotalDuration() {
131 | if (mMarkers.size() == 0) {
132 | return 0;
133 | }
134 |
135 | long first = mMarkers.get(0).time;
136 | long last = mMarkers.get(mMarkers.size() - 1).time;
137 | return last - first;
138 | }
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android Remote Config Library
2 |
3 | [](https://lgtm.com/projects/g/gayanvoice/remote-config/context:java) [](https://travis-ci.org/gayanvoice/remote-config) [](https://lgtm.com/projects/g/gayanvoice/android-remote-config-library/alerts/) [](https://jitpack.io/#gayanvoice/android-remote-config-library) [](https://android-arsenal.com/api?level=14) 
4 |
5 | A cool alternative for Google firebase remote-config library! Remote config the variables, appearance and the behavior of your app without publishing an app update. The library is based on Google Volley library.
6 |
7 |
8 |
9 | Library retrieves the JSON file from https://github.com/gayanvoice/android-remote-config-library/blob/master/remote-config.json.
10 | To configure the data in the JSON file, you can `fork` the library and change the data in the JSON file.
11 |
12 | ## Get
13 | ### Gradle
14 | 1. Add this to `build.gradle` of project gradle dependency
15 | ```groovy
16 | allprojects {
17 | repositories {
18 | ...
19 | maven { url 'https://jitpack.io' }
20 | }
21 | }
22 | ```
23 | 2. Add this to `build.gradle` of app gradle dependency
24 | ```groovy
25 | dependencies {
26 | implementation 'com.github.gayanvoice:android-remote-config-library-firebase:1.0.3'
27 | }
28 | ```
29 | ### Or
30 | ### Maven
31 | 1. Add this to `build.gradle` of project gradle dependency
32 |
33 | ```xml
34 |
35 |
36 | jitpack.io
37 | https://jitpack.io
38 |
39 |
40 | ```
41 | 2. Add this to `build.gradle` of module gradle dependency
42 | ```xml
43 |
44 | com.github.gayanvoice
45 | remote-config
46 | 1.0.3
47 |
48 | ```
49 | ## Usage
50 | #### Set internet permission
51 | ```groovy
52 |
53 | ```
54 | ### Java
55 | #### Import remote config library
56 |
57 | ```java
58 | import com.remoteconfig.library.*;
59 | ```
60 | #### Set request
61 | ```java
62 | // set request
63 | RequestQueue queue = FetchRemote.newRequestQueue(MainActivity.this);
64 |
65 | // url of the json file
66 | String url ="https://raw.githubusercontent.com/gayanvoice/remote-config/master/remote-config.json";
67 |
68 | // request the json file
69 | RemoteConfig remoteConfig = new RemoteConfig(MainActivity.this, url,
70 | new Response.Listener() {
71 | @Override
72 | public void onComplete() {
73 | // json file retrieved
74 |
75 | // declare remote param
76 | RemoteParams remoteParams = new RemoteParams(MainActivity.this);
77 |
78 | // get String value
79 | String stringValue = remoteParams.getString("short_text", "default_text");
80 |
81 | // get int values
82 | int intValue = remoteParams.getInt("number", 200);
83 |
84 | // get JSON Object
85 | JSONObject jsonObject = remoteParams.getJSONObject("json_object");
86 |
87 | // get JSON Array
88 | JSONArray jsonArray = remoteParams.getJSONArray("json_array");
89 |
90 | // get boolean value
91 | boolean booleanValue = remoteParams.getBoolean("boolean", false);
92 | }
93 | },
94 | new Response.ErrorListener() {
95 | @Override
96 | public void onError(RemoteError error) {
97 | // json file retrieve error
98 | }
99 | }
100 | );
101 |
102 | // clear cache
103 | remoteConfig.setShouldCache(false);
104 | queue.add(remoteConfig);
105 | ```
106 | ### Kotlin
107 | #### Import remote config library
108 | ```java
109 | import com.remoteconfig.library.*
110 | ```
111 | #### Set request
112 | ```kotlin
113 | // set request
114 | val queue = FetchRemote.newRequestQueue(this@MainActivity)
115 |
116 | // url of the json file
117 | val url = "https://raw.githubusercontent.com/gayanvoice/remote-config/master/remote-config.json"
118 |
119 | // request the json file
120 | val remoteConfig = RemoteConfig(this@MainActivity, url,
121 | Response.Listener {
122 | // json file retrieved
123 |
124 | // declare remote param
125 | val remoteParams = RemoteParams(this@MainActivity)
126 |
127 | // get String value
128 | val stringValue = remoteParams.getString("short_text", "default_text")
129 |
130 | // get int values
131 | val intValue = remoteParams.getInt("number", 200)
132 |
133 | // get JSON Object
134 | val jsonObject = remoteParams.getJSONObject("json_object")
135 |
136 | // get JSON Array
137 | val jsonArray = remoteParams.getJSONArray("json_array")
138 |
139 | // get boolean value
140 | val booleanValue = remoteParams.getBoolean("boolean", false)
141 | },
142 | Response.ErrorListener {
143 | // json file retrieve error
144 | }
145 | )
146 |
147 | // clear cache
148 | remoteConfig.setShouldCache(false)
149 | queue.add(remoteConfig)
150 | ```
151 | ## Develop the library
152 |
153 | 1. Select `Git` from `Check out project from Version Control` in your Android Studio
154 | 2. Paste the repository url and click `Clone` button
155 | 3. Click `Yes` to open the repository
156 | 4. `Build` using the latest `Gradle` version
157 |
158 | Go to https://github.com/gayanvoice/android-vpn-client-ics-openvpn#develop see the steps
159 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/ByteArrayPool.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 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.remoteconfig.library.toolbox;
18 |
19 | import java.util.ArrayList;
20 | import java.util.Collections;
21 | import java.util.Comparator;
22 | import java.util.List;
23 |
24 | /**
25 | * ByteArrayPool is a source and repository of byte[] objects. Its purpose is to supply
26 | * those buffers to consumers who need to use them for a short period of time and then dispose of
27 | * them. Simply creating and disposing such buffers in the conventional manner can considerable heap
28 | * churn and garbage collection delays on Android, which lacks good management of short-lived heap
29 | * objects. It may be advantageous to trade off some memory in the form of a permanently allocated
30 | * pool of buffers in order to gain heap performance improvements; that is what this class does.
31 | *
32 | *
A good candidate user for this class is something like an I/O system that uses large temporary
33 | * byte[] buffers to copy data around. In these use cases, often the consumer wants the
34 | * buffer to be a certain minimum size to ensure good performance (e.g. when copying data chunks off
35 | * of a stream), but doesn't mind if the buffer is larger than the minimum. Taking this into account
36 | * and also to maximize the odds of being able to reuse a recycled buffer, this class is free to
37 | * return buffers larger than the requested size. The caller needs to be able to gracefully deal
38 | * with getting buffers any size over the minimum.
39 | *
40 | *
If there is not a suitably-sized buffer in its recycling pool when a buffer is requested, this
41 | * class will allocate a new buffer and return it.
42 | *
43 | *
This class has no special ownership of buffers it creates; the caller is free to take a buffer
44 | * it receives from this pool, use it permanently, and never return it to the pool; additionally, it
45 | * is not harmful to return to this pool a buffer that was allocated elsewhere, provided there are
46 | * no other lingering references to it.
47 | *
48 | *
This class ensures that the total size of the buffers in its recycling pool never exceeds a
49 | * certain byte limit. When a buffer is returned that would cause the pool to exceed the limit,
50 | * least-recently-used buffers are disposed.
51 | */
52 | public class ByteArrayPool {
53 | /** The buffer pool, arranged both by last use and by buffer size */
54 | private final List mBuffersByLastUse = new ArrayList<>();
55 |
56 | private final List mBuffersBySize = new ArrayList<>(64);
57 |
58 | /** The total size of the buffers in the pool */
59 | private int mCurrentSize = 0;
60 |
61 | /**
62 | * The maximum aggregate size of the buffers in the pool. Old buffers are discarded to stay
63 | * under this limit.
64 | */
65 | private final int mSizeLimit;
66 |
67 | /** Compares buffers by size */
68 | protected static final Comparator BUF_COMPARATOR =
69 | new Comparator() {
70 | @Override
71 | public int compare(byte[] lhs, byte[] rhs) {
72 | return lhs.length - rhs.length;
73 | }
74 | };
75 |
76 | /** @param sizeLimit the maximum size of the pool, in bytes */
77 | public ByteArrayPool(int sizeLimit) {
78 | mSizeLimit = sizeLimit;
79 | }
80 |
81 | /**
82 | * Returns a buffer from the pool if one is available in the requested size, or allocates a new
83 | * one if a pooled one is not available.
84 | *
85 | * @param len the minimum size, in bytes, of the requested buffer. The returned buffer may be
86 | * larger.
87 | * @return a byte[] buffer is always returned.
88 | */
89 | public synchronized byte[] getBuf(int len) {
90 | for (int i = 0; i < mBuffersBySize.size(); i++) {
91 | byte[] buf = mBuffersBySize.get(i);
92 | if (buf.length >= len) {
93 | mCurrentSize -= buf.length;
94 | mBuffersBySize.remove(i);
95 | mBuffersByLastUse.remove(buf);
96 | return buf;
97 | }
98 | }
99 | return new byte[len];
100 | }
101 |
102 | /**
103 | * Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted
104 | * size.
105 | *
106 | * @param buf the buffer to return to the pool.
107 | */
108 | public synchronized void returnBuf(byte[] buf) {
109 | if (buf == null || buf.length > mSizeLimit) {
110 | return;
111 | }
112 | mBuffersByLastUse.add(buf);
113 | int pos = Collections.binarySearch(mBuffersBySize, buf, BUF_COMPARATOR);
114 | if (pos < 0) {
115 | pos = -pos - 1;
116 | }
117 | mBuffersBySize.add(pos, buf);
118 | mCurrentSize += buf.length;
119 | trim();
120 | }
121 |
122 | /** Removes buffers from the pool until it is under its size limit. */
123 | private synchronized void trim() {
124 | while (mCurrentSize > mSizeLimit) {
125 | byte[] buf = mBuffersByLastUse.remove(0);
126 | mBuffersBySize.remove(buf);
127 | mCurrentSize -= buf.length;
128 | }
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/NetworkResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import java.net.HttpURLConnection;
20 | import java.util.ArrayList;
21 | import java.util.Collections;
22 | import java.util.List;
23 | import java.util.Map;
24 | import java.util.TreeMap;
25 |
26 | //performRequest
27 | /** Data and headers returned from {@link Network#performRequest(Request)}. */
28 | public class NetworkResponse {
29 |
30 | /**
31 | * Creates a new network response.
32 | *
33 | * @param statusCode the HTTP status code
34 | * @param data Response body
35 | * @param headers Headers returned with this response, or null for none
36 | * @param notModified True if the server returned a 304 and the data was already in cache
37 | * @param networkTimeMs Round-trip network time to receive network response
38 | * @deprecated see {@link #NetworkResponse(int, byte[], boolean, long, List)}. This constructor
39 | * cannot handle server responses containing multiple headers with the same name. This
40 | * constructor may be removed in a future release of FetchRemote.
41 | */
42 | @Deprecated
43 | public NetworkResponse(
44 | int statusCode,
45 | byte[] data,
46 | Map headers,
47 | boolean notModified,
48 | long networkTimeMs) {
49 | this(statusCode, data, headers, toAllHeaderList(headers), notModified, networkTimeMs);
50 | }
51 |
52 | /**
53 | * Creates a new network response.
54 | *
55 | * @param statusCode the HTTP status code
56 | * @param data Response body
57 | * @param notModified True if the server returned a 304 and the data was already in cache
58 | * @param networkTimeMs Round-trip network time to receive network response
59 | * @param allHeaders All headers returned with this response, or null for none
60 | */
61 | public NetworkResponse(
62 | int statusCode,
63 | byte[] data,
64 | boolean notModified,
65 | long networkTimeMs,
66 | List allHeaders) {
67 | this(statusCode, data, toHeaderMap(allHeaders), allHeaders, notModified, networkTimeMs);
68 | }
69 |
70 | /**
71 | * Creates a new network response.
72 | *
73 | * @param statusCode the HTTP status code
74 | * @param data Response body
75 | * @param headers Headers returned with this response, or null for none
76 | * @param notModified True if the server returned a 304 and the data was already in cache
77 | * @deprecated see {@link #NetworkResponse(int, byte[], boolean, long, List)}. This constructor
78 | * cannot handle server responses containing multiple headers with the same name. This
79 | * constructor may be removed in a future release of FetchRemote.
80 | */
81 | @Deprecated
82 | public NetworkResponse(
83 | int statusCode, byte[] data, Map headers, boolean notModified) {
84 | this(statusCode, data, headers, notModified, /* networkTimeMs= */ 0);
85 | }
86 |
87 | /**
88 | * Creates a new network response for an OK response with no headers.
89 | *
90 | * @param data Response body
91 | */
92 | public NetworkResponse(byte[] data) {
93 | this(
94 | HttpURLConnection.HTTP_OK,
95 | data,
96 | /* notModified= */ false,
97 | /* networkTimeMs= */ 0,
98 | Collections.emptyList());
99 | }
100 |
101 | /**
102 | * Creates a new network response for an OK response.
103 | *
104 | * @param data Response body
105 | * @param headers Headers returned with this response, or null for none
106 | * @deprecated see {@link #NetworkResponse(int, byte[], boolean, long, List)}. This constructor
107 | * cannot handle server responses containing multiple headers with the same name. This
108 | * constructor may be removed in a future release of FetchRemote.
109 | */
110 | @Deprecated
111 | public NetworkResponse(byte[] data, Map headers) {
112 | this(
113 | HttpURLConnection.HTTP_OK,
114 | data,
115 | headers,
116 | /* notModified= */ false,
117 | /* networkTimeMs= */ 0);
118 | }
119 |
120 | private NetworkResponse(
121 | int statusCode,
122 | byte[] data,
123 | Map headers,
124 | List allHeaders,
125 | boolean notModified,
126 | long networkTimeMs) {
127 | this.statusCode = statusCode;
128 | this.data = data;
129 | this.headers = headers;
130 | if (allHeaders == null) {
131 | this.allHeaders = null;
132 | } else {
133 | this.allHeaders = Collections.unmodifiableList(allHeaders);
134 | }
135 | this.notModified = notModified;
136 | this.networkTimeMs = networkTimeMs;
137 | }
138 |
139 | /** The HTTP status code. */
140 | public final int statusCode;
141 |
142 | /** Raw data from this response. */
143 | public final byte[] data;
144 |
145 | /**
146 | * Response headers.
147 | *
148 | *
This map is case-insensitive. It should not be mutated directly.
149 | *
150 | *
Note that if the server returns two headers with the same (case-insensitive) name, this
151 | * map will only contain the last one. Use {@link #allHeaders} to inspect all headers returned
152 | * by the server.
153 | */
154 | public final Map headers;
155 |
156 | /** All response headers. Must not be mutated directly. */
157 | public final List allHeaders;
158 |
159 | /** True if the server returned a 304 (Not Modified). */
160 | public final boolean notModified;
161 |
162 | /** Network roundtrip time in milliseconds. */
163 | public final long networkTimeMs;
164 |
165 | private static Map toHeaderMap(List allHeaders) {
166 | if (allHeaders == null) {
167 | return null;
168 | }
169 | if (allHeaders.isEmpty()) {
170 | return Collections.emptyMap();
171 | }
172 | Map headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
173 | // Later elements in the list take precedence.
174 | for (Header header : allHeaders) {
175 | headers.put(header.getName(), header.getValue());
176 | }
177 | return headers;
178 | }
179 |
180 | private static List toAllHeaderList(Map headers) {
181 | if (headers == null) {
182 | return null;
183 | }
184 | if (headers.isEmpty()) {
185 | return Collections.emptyList();
186 | }
187 | List allHeaders = new ArrayList<>(headers.size());
188 | for (Map.Entry header : headers.entrySet()) {
189 | allHeaders.add(new Header(header.getKey(), header.getValue()));
190 | }
191 | return allHeaders;
192 | }
193 | }
194 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/NetworkDispatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import android.annotation.TargetApi;
20 | import android.net.TrafficStats;
21 | import android.os.Build;
22 | import android.os.Process;
23 | import android.os.SystemClock;
24 | import androidx.annotation.VisibleForTesting;
25 |
26 |
27 | import java.util.concurrent.BlockingQueue;
28 |
29 | /**
30 | * Provides a thread for performing network dispatch from a queue of requests.
31 | *
32 | *
Requests added to the specified queue are processed from the network via a specified {@link
33 | * Network} interface. Responses are committed to cache, if eligible, using a specified {@link
34 | * Cache} interface. Valid responses and errors are posted back to the caller via a {@link
35 | * ResponseDelivery}.
36 | */
37 | public class NetworkDispatcher extends Thread {
38 |
39 | /** The queue of requests to service. */
40 | private final BlockingQueue> mQueue;
41 | /** The network interface for processing requests. */
42 | private final Network mNetwork;
43 | /** The cache to write to. */
44 | private final Cache mCache;
45 | /** For posting responses and errors. */
46 | private final ResponseDelivery mDelivery;
47 | /** Used for telling us to die. */
48 | private volatile boolean mQuit = false;
49 |
50 | /**
51 | * Creates a new network dispatcher thread. You must call {@link #start()} in order to begin
52 | * processing.
53 | *
54 | * @param queue Queue of incoming requests for triage
55 | * @param network Network interface to use for performing requests
56 | * @param cache Cache interface to use for writing responses to cache
57 | * @param delivery Delivery interface to use for posting responses
58 | */
59 | public NetworkDispatcher(
60 | BlockingQueue> queue,
61 | Network network,
62 | Cache cache,
63 | ResponseDelivery delivery) {
64 | mQueue = queue;
65 | mNetwork = network;
66 | mCache = cache;
67 | mDelivery = delivery;
68 | }
69 |
70 | /**
71 | * Forces this dispatcher to quit immediately. If any requests are still in the queue, they are
72 | * not guaranteed to be processed.
73 | */
74 | public void quit() {
75 | mQuit = true;
76 | interrupt();
77 | }
78 |
79 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
80 | private void addTrafficStatsTag(Request> request) {
81 | // Tag the request (if API >= 14)
82 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
83 | TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
84 | }
85 | }
86 |
87 | @Override
88 | public void run() {
89 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
90 | while (true) {
91 | try {
92 | processRequest();
93 | } catch (InterruptedException e) {
94 | // We may have been interrupted because it was time to quit.
95 | if (mQuit) {
96 | Thread.currentThread().interrupt();
97 | return;
98 | }
99 | VolleyLog.e(
100 | "Ignoring spurious interrupt of NetworkDispatcher thread; "
101 | + "use quit() to terminate it");
102 | }
103 | }
104 | }
105 |
106 | // Extracted to its own method to ensure locals have a constrained liveness scope by the GC.
107 | // This is needed to avoid keeping previous request references alive for an indeterminate amount
108 | // of time. Update consumer-proguard-rules.pro when modifying this. See also
109 | // https://github.com/google/volley/issues/114
110 | private void processRequest() throws InterruptedException {
111 | // Take a request from the queue.
112 | Request> request = mQueue.take();
113 | processRequest(request);
114 | }
115 |
116 | @VisibleForTesting
117 | void processRequest(Request> request) {
118 | long startTimeMs = SystemClock.elapsedRealtime();
119 | request.sendEvent(RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED);
120 | try {
121 | request.addMarker("network-queue-take");
122 |
123 | // If the request was cancelled already, do not perform the
124 | // network request.
125 | if (request.isCanceled()) {
126 | request.finish("network-discard-cancelled");
127 | request.notifyListenerResponseNotUsable();
128 | return;
129 | }
130 |
131 | addTrafficStatsTag(request);
132 |
133 | // Perform the network request.
134 | NetworkResponse networkResponse = mNetwork.performRequest(request);
135 | request.addMarker("network-http-complete");
136 |
137 | // If the server returned 304 AND we delivered a response already,
138 | // we're done -- don't deliver a second identical response.
139 | if (networkResponse.notModified && request.hasHadResponseDelivered()) {
140 | request.finish("not-modified");
141 | request.notifyListenerResponseNotUsable();
142 | return;
143 | }
144 |
145 | // Parse the response here on the worker thread.
146 | Response> response = request.parseNetworkResponse(networkResponse);
147 | request.addMarker("network-parse-complete");
148 |
149 | // Write to cache if applicable.
150 | // TODO: Only update cache metadata instead of entire record for 304s.
151 | if (request.shouldCache() && response.cacheEntry != null) {
152 | mCache.put(request.getCacheKey(), response.cacheEntry);
153 | request.addMarker("network-cache-written");
154 | }
155 |
156 | // Post the response back.
157 | request.markDelivered();
158 | mDelivery.postResponse(request, response);
159 | request.notifyListenerResponseReceived(response);
160 | } catch (RemoteError remoteError) {
161 | remoteError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
162 | parseAndDeliverNetworkError(request, remoteError);
163 | request.notifyListenerResponseNotUsable();
164 | } catch (Exception e) {
165 | VolleyLog.e(e, "Unhandled exception %s", e.toString());
166 | RemoteError remoteError = new RemoteError(e);
167 | remoteError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
168 | mDelivery.postError(request, remoteError);
169 | request.notifyListenerResponseNotUsable();
170 | } finally {
171 | request.sendEvent(RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_FINISHED);
172 | }
173 | }
174 |
175 | private void parseAndDeliverNetworkError(Request> request, RemoteError error) {
176 | error = request.parseNetworkError(error);
177 | mDelivery.postError(request, error);
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/HttpHeaderParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import com.remoteconfig.library.Cache;
20 | import com.remoteconfig.library.Header;
21 | import com.remoteconfig.library.NetworkResponse;
22 | import com.remoteconfig.library.VolleyLog;
23 |
24 | import java.text.ParseException;
25 | import java.text.SimpleDateFormat;
26 | import java.util.ArrayList;
27 | import java.util.Date;
28 | import java.util.List;
29 | import java.util.Locale;
30 | import java.util.Map;
31 | import java.util.TimeZone;
32 | import java.util.TreeMap;
33 |
34 | /** Utility methods for parsing HTTP headers. */
35 | public class HttpHeaderParser {
36 |
37 | static final String HEADER_CONTENT_TYPE = "Content-Type";
38 |
39 | private static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
40 |
41 | private static final String RFC1123_PARSE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
42 |
43 | // Hardcode 'GMT' rather than using 'zzz' since some platforms append an extraneous +00:00.
44 | // See #287.
45 | private static final String RFC1123_OUTPUT_FORMAT = "EEE, dd MMM yyyy HH:mm:ss 'GMT'";
46 |
47 | /**
48 | * Extracts a {@link Cache.Entry} from a {@link NetworkResponse}.
49 | *
50 | * @param response The network response to parse headers from
51 | * @return a cache entry for the given response, or null if the response is not cacheable.
52 | */
53 | public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
54 | long now = System.currentTimeMillis();
55 |
56 | Map headers = response.headers;
57 |
58 | long serverDate = 0;
59 | long lastModified = 0;
60 | long serverExpires = 0;
61 | long softExpire = 0;
62 | long finalExpire = 0;
63 | long maxAge = 0;
64 | long staleWhileRevalidate = 0;
65 | boolean hasCacheControl = false;
66 | boolean mustRevalidate = false;
67 |
68 | String serverEtag = null;
69 | String headerValue;
70 |
71 | headerValue = headers.get("Date");
72 | if (headerValue != null) {
73 | serverDate = parseDateAsEpoch(headerValue);
74 | }
75 |
76 | headerValue = headers.get("Cache-Control");
77 | if (headerValue != null) {
78 | hasCacheControl = true;
79 | String[] tokens = headerValue.split(",", 0);
80 | for (int i = 0; i < tokens.length; i++) {
81 | String token = tokens[i].trim();
82 | if (token.equals("no-cache") || token.equals("no-store")) {
83 | return null;
84 | } else if (token.startsWith("max-age=")) {
85 | try {
86 | maxAge = Long.parseLong(token.substring(8));
87 | } catch (Exception e) {
88 | }
89 | } else if (token.startsWith("stale-while-revalidate=")) {
90 | try {
91 | staleWhileRevalidate = Long.parseLong(token.substring(23));
92 | } catch (Exception e) {
93 | }
94 | } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
95 | mustRevalidate = true;
96 | }
97 | }
98 | }
99 |
100 | headerValue = headers.get("Expires");
101 | if (headerValue != null) {
102 | serverExpires = parseDateAsEpoch(headerValue);
103 | }
104 |
105 | headerValue = headers.get("Last-Modified");
106 | if (headerValue != null) {
107 | lastModified = parseDateAsEpoch(headerValue);
108 | }
109 |
110 | serverEtag = headers.get("ETag");
111 |
112 | // Cache-Control takes precedence over an Expires header, even if both exist and Expires
113 | // is more restrictive.
114 | if (hasCacheControl) {
115 | softExpire = now + maxAge * 1000;
116 | finalExpire = mustRevalidate ? softExpire : softExpire + staleWhileRevalidate * 1000;
117 | } else if (serverDate > 0 && serverExpires >= serverDate) {
118 | // Default semantic for Expire header in HTTP specification is softExpire.
119 | softExpire = now + (serverExpires - serverDate);
120 | finalExpire = softExpire;
121 | }
122 |
123 | Cache.Entry entry = new Cache.Entry();
124 | entry.data = response.data;
125 | entry.etag = serverEtag;
126 | entry.softTtl = softExpire;
127 | entry.ttl = finalExpire;
128 | entry.serverDate = serverDate;
129 | entry.lastModified = lastModified;
130 | entry.responseHeaders = headers;
131 | entry.allResponseHeaders = response.allHeaders;
132 |
133 | return entry;
134 | }
135 |
136 | /** Parse date in RFC1123 format, and return its value as epoch */
137 | public static long parseDateAsEpoch(String dateStr) {
138 | try {
139 | // Parse date in RFC1123 format if this header contains one
140 | return newUsGmtFormatter(RFC1123_PARSE_FORMAT).parse(dateStr).getTime();
141 | } catch (ParseException e) {
142 | // Date in invalid format, fallback to 0
143 | // If the value is either "0" or "-1" we only log to verbose,
144 | // these values are pretty common and cause log spam.
145 | String message = "Unable to parse dateStr: %s, falling back to 0";
146 | if ("0".equals(dateStr) || "-1".equals(dateStr)) {
147 | VolleyLog.v(message, dateStr);
148 | } else {
149 | VolleyLog.e(e, message, dateStr);
150 | }
151 |
152 | return 0;
153 | }
154 | }
155 |
156 | /** Format an epoch date in RFC1123 format. */
157 | static String formatEpochAsRfc1123(long epoch) {
158 | return newUsGmtFormatter(RFC1123_OUTPUT_FORMAT).format(new Date(epoch));
159 | }
160 |
161 | private static SimpleDateFormat newUsGmtFormatter(String format) {
162 | SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
163 | formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
164 | return formatter;
165 | }
166 |
167 | /**
168 | * Retrieve a charset from headers
169 | *
170 | * @param headers An {@link Map} of headers
171 | * @param defaultCharset Charset to return if none can be found
172 | * @return Returns the charset specified in the Content-Type of this header, or the
173 | * defaultCharset if none can be found.
174 | */
175 | public static String parseCharset(Map headers, String defaultCharset) {
176 | String contentType = headers.get(HEADER_CONTENT_TYPE);
177 | if (contentType != null) {
178 | String[] params = contentType.split(";", 0);
179 | for (int i = 1; i < params.length; i++) {
180 | String[] pair = params[i].trim().split("=", 0);
181 | if (pair.length == 2) {
182 | if (pair[0].equals("charset")) {
183 | return pair[1];
184 | }
185 | }
186 | }
187 | }
188 |
189 | return defaultCharset;
190 | }
191 |
192 | /**
193 | * Returns the charset specified in the Content-Type of this header, or the HTTP default
194 | * (ISO-8859-1) if none can be found.
195 | */
196 | public static String parseCharset(Map headers) {
197 | return parseCharset(headers, DEFAULT_CONTENT_CHARSET);
198 | }
199 |
200 | // Note - these are copied from NetworkResponse to avoid making them public (as needed to access
201 | // them from the .toolbox package), which would mean they'd become part of the FetchRemote API.
202 | // TODO: Consider obfuscating official releases so we can share utility methods between FetchRemote
203 | // and Toolbox without making them public APIs.
204 |
205 | static Map toHeaderMap(List allHeaders) {
206 | Map headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
207 | // Later elements in the list take precedence.
208 | for (Header header : allHeaders) {
209 | headers.put(header.getName(), header.getValue());
210 | }
211 | return headers;
212 | }
213 |
214 | static List toAllHeaderList(Map headers) {
215 | List allHeaders = new ArrayList<>(headers.size());
216 | for (Map.Entry header : headers.entrySet()) {
217 | allHeaders.add(new Header(header.getKey(), header.getValue()));
218 | }
219 | return allHeaders;
220 | }
221 | }
222 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/HurlStack.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import androidx.annotation.VisibleForTesting;
20 | import com.remoteconfig.library.AuthFailureError;
21 | import com.remoteconfig.library.Header;
22 | import com.remoteconfig.library.Request;
23 | import com.remoteconfig.library.Request.Method;
24 |
25 | import java.io.DataOutputStream;
26 | import java.io.FilterInputStream;
27 | import java.io.IOException;
28 | import java.io.InputStream;
29 | import java.net.HttpURLConnection;
30 | import java.net.URL;
31 | import java.util.ArrayList;
32 | import java.util.HashMap;
33 | import java.util.List;
34 | import java.util.Map;
35 | import javax.net.ssl.HttpsURLConnection;
36 | import javax.net.ssl.SSLSocketFactory;
37 |
38 | /** A {@link BaseHttpStack} based on {@link HttpURLConnection}. */
39 | public class HurlStack extends BaseHttpStack {
40 |
41 | private static final int HTTP_CONTINUE = 100;
42 |
43 | /** An interface for transforming URLs before use. */
44 | public interface UrlRewriter {
45 | /**
46 | * Returns a URL to use instead of the provided one, or null to indicate this URL should not
47 | * be used at all.
48 | */
49 | String rewriteUrl(String originalUrl);
50 | }
51 |
52 | private final UrlRewriter mUrlRewriter;
53 | private final SSLSocketFactory mSslSocketFactory;
54 |
55 | public HurlStack() {
56 | this(/* urlRewriter = */ null);
57 | }
58 |
59 | /** @param urlRewriter Rewriter to use for request URLs */
60 | public HurlStack(UrlRewriter urlRewriter) {
61 | this(urlRewriter, /* sslSocketFactory = */ null);
62 | }
63 |
64 | /**
65 | * @param urlRewriter Rewriter to use for request URLs
66 | * @param sslSocketFactory SSL factory to use for HTTPS connections
67 | */
68 | public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) {
69 | mUrlRewriter = urlRewriter;
70 | mSslSocketFactory = sslSocketFactory;
71 | }
72 |
73 | @Override
74 | public HttpResponse executeRequest(Request> request, Map additionalHeaders)
75 | throws IOException, AuthFailureError {
76 | String url = request.getUrl();
77 | HashMap map = new HashMap<>();
78 | map.putAll(additionalHeaders);
79 | // Request.getHeaders() takes precedence over the given additional (cache) headers).
80 | map.putAll(request.getHeaders());
81 | if (mUrlRewriter != null) {
82 | String rewritten = mUrlRewriter.rewriteUrl(url);
83 | if (rewritten == null) {
84 | throw new IOException("URL blocked by rewriter: " + url);
85 | }
86 | url = rewritten;
87 | }
88 | URL parsedUrl = new URL(url);
89 | HttpURLConnection connection = openConnection(parsedUrl, request);
90 | boolean keepConnectionOpen = false;
91 | try {
92 | for (String headerName : map.keySet()) {
93 | connection.setRequestProperty(headerName, map.get(headerName));
94 | }
95 | setConnectionParametersForRequest(connection, request);
96 | // Initialize HttpResponse with data from the HttpURLConnection.
97 | int responseCode = connection.getResponseCode();
98 | if (responseCode == -1) {
99 | // -1 is returned by getResponseCode() if the response code could not be retrieved.
100 | // Signal to the caller that something was wrong with the connection.
101 | throw new IOException("Could not retrieve response code from HttpUrlConnection.");
102 | }
103 |
104 | if (!hasResponseBody(request.getMethod(), responseCode)) {
105 | return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()));
106 | }
107 |
108 | // Need to keep the connection open until the stream is consumed by the caller. Wrap the
109 | // stream such that close() will disconnect the connection.
110 | keepConnectionOpen = true;
111 | return new HttpResponse(
112 | responseCode,
113 | convertHeaders(connection.getHeaderFields()),
114 | connection.getContentLength(),
115 | new UrlConnectionInputStream(connection));
116 | } finally {
117 | if (!keepConnectionOpen) {
118 | connection.disconnect();
119 | }
120 | }
121 | }
122 |
123 | @VisibleForTesting
124 | static List convertHeaders(Map> responseHeaders) {
125 | List headerList = new ArrayList<>(responseHeaders.size());
126 | for (Map.Entry> entry : responseHeaders.entrySet()) {
127 | // HttpUrlConnection includes the status line as a header with a null key; omit it here
128 | // since it's not really a header and the rest of FetchRemote assumes non-null keys.
129 | if (entry.getKey() != null) {
130 | for (String value : entry.getValue()) {
131 | headerList.add(new Header(entry.getKey(), value));
132 | }
133 | }
134 | }
135 | return headerList;
136 | }
137 |
138 | /**
139 | * Checks if a response message contains a body.
140 | *
141 | * @see RFC 7230 section 3.3
142 | * @param requestMethod request method
143 | * @param responseCode response status code
144 | * @return whether the response has a body
145 | */
146 | private static boolean hasResponseBody(int requestMethod, int responseCode) {
147 | return requestMethod != Method.HEAD
148 | && !(HTTP_CONTINUE <= responseCode && responseCode < HttpURLConnection.HTTP_OK)
149 | && responseCode != HttpURLConnection.HTTP_NO_CONTENT
150 | && responseCode != HttpURLConnection.HTTP_NOT_MODIFIED;
151 | }
152 |
153 | /**
154 | * Wrapper for a {@link HttpURLConnection}'s InputStream which disconnects the connection on
155 | * stream close.
156 | */
157 | static class UrlConnectionInputStream extends FilterInputStream {
158 | private final HttpURLConnection mConnection;
159 |
160 | UrlConnectionInputStream(HttpURLConnection connection) {
161 | super(inputStreamFromConnection(connection));
162 | mConnection = connection;
163 | }
164 |
165 | @Override
166 | public void close() throws IOException {
167 | super.close();
168 | mConnection.disconnect();
169 | }
170 | }
171 |
172 | /**
173 | * Initializes an {@link InputStream} from the given {@link HttpURLConnection}.
174 | *
175 | * @param connection create a HttpURL connection
176 | * @return an HttpEntity populated with data from connection.
177 | */
178 | private static InputStream inputStreamFromConnection(HttpURLConnection connection) {
179 | InputStream inputStream;
180 | try {
181 | inputStream = connection.getInputStream();
182 | } catch (IOException ioe) {
183 | inputStream = connection.getErrorStream();
184 | }
185 | return inputStream;
186 | }
187 |
188 | /** Create an {@link HttpURLConnection} for the specified {@code url}. */
189 | protected HttpURLConnection createConnection(URL url) throws IOException {
190 | HttpURLConnection connection = (HttpURLConnection) url.openConnection();
191 |
192 | // Workaround for the M release HttpURLConnection not observing the
193 | // HttpURLConnection.setFollowRedirects() property.
194 | // https://code.google.com/p/android/issues/detail?id=194495
195 | connection.setInstanceFollowRedirects(HttpURLConnection.getFollowRedirects());
196 |
197 | return connection;
198 | }
199 |
200 | /**
201 | * Opens an {@link HttpURLConnection} with parameters.
202 | *
203 | * @param url of the remote
204 | * @return an open connection
205 | * @throws IOException of the method
206 | */
207 | private HttpURLConnection openConnection(URL url, Request> request) throws IOException {
208 | HttpURLConnection connection = createConnection(url);
209 |
210 | int timeoutMs = request.getTimeoutMs();
211 | connection.setConnectTimeout(timeoutMs);
212 | connection.setReadTimeout(timeoutMs);
213 | connection.setUseCaches(false);
214 | connection.setDoInput(true);
215 |
216 | // use caller-provided custom SslSocketFactory, if any, for HTTPS
217 | if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
218 | ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory);
219 | }
220 |
221 | return connection;
222 | }
223 |
224 | // NOTE: Any request headers added here (via setRequestProperty or addRequestProperty) should be
225 | // checked against the existing properties in the connection and not overridden if already set.
226 | @SuppressWarnings("deprecation")
227 | /* package */ static void setConnectionParametersForRequest(
228 | HttpURLConnection connection, Request> request) throws IOException, AuthFailureError {
229 | switch (request.getMethod()) {
230 | case Method.DEPRECATED_GET_OR_POST:
231 | // This is the deprecated way that needs to be handled for backwards compatibility.
232 | // If the request's post body is null, then the assumption is that the request is
233 | // GET. Otherwise, it is assumed that the request is a POST.
234 | byte[] postBody = request.getPostBody();
235 | if (postBody != null) {
236 | connection.setRequestMethod("POST");
237 | addBody(connection, request, postBody);
238 | }
239 | break;
240 | case Method.GET:
241 | // Not necessary to set the request method because connection defaults to GET but
242 | // being explicit here.
243 | connection.setRequestMethod("GET");
244 | break;
245 | case Method.DELETE:
246 | connection.setRequestMethod("DELETE");
247 | break;
248 | case Method.POST:
249 | connection.setRequestMethod("POST");
250 | addBodyIfExists(connection, request);
251 | break;
252 | case Method.PUT:
253 | connection.setRequestMethod("PUT");
254 | addBodyIfExists(connection, request);
255 | break;
256 | case Method.HEAD:
257 | connection.setRequestMethod("HEAD");
258 | break;
259 | case Method.OPTIONS:
260 | connection.setRequestMethod("OPTIONS");
261 | break;
262 | case Method.TRACE:
263 | connection.setRequestMethod("TRACE");
264 | break;
265 | case Method.PATCH:
266 | connection.setRequestMethod("PATCH");
267 | addBodyIfExists(connection, request);
268 | break;
269 | default:
270 | throw new IllegalStateException("Unknown method type.");
271 | }
272 | }
273 |
274 | private static void addBodyIfExists(HttpURLConnection connection, Request> request)
275 | throws IOException, AuthFailureError {
276 | byte[] body = request.getBody();
277 | if (body != null) {
278 | addBody(connection, request, body);
279 | }
280 | }
281 |
282 | private static void addBody(HttpURLConnection connection, Request> request, byte[] body)
283 | throws IOException {
284 | // Prepare output. There is no need to set Content-Length explicitly,
285 | // since this is handled by HttpURLConnection using the size of the prepared
286 | // output stream.
287 | connection.setDoOutput(true);
288 | // Set the content-type unless it was already set (by Request#getHeaders).
289 | if (!connection.getRequestProperties().containsKey(HttpHeaderParser.HEADER_CONTENT_TYPE)) {
290 | connection.setRequestProperty(
291 | HttpHeaderParser.HEADER_CONTENT_TYPE, request.getBodyContentType());
292 | }
293 | DataOutputStream out = new DataOutputStream(connection.getOutputStream());
294 | out.write(body);
295 | out.close();
296 | }
297 | }
298 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/RequestQueue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import android.os.Handler;
20 | import android.os.Looper;
21 | import androidx.annotation.IntDef;
22 |
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.util.ArrayList;
26 | import java.util.HashSet;
27 | import java.util.List;
28 | import java.util.Set;
29 | import java.util.concurrent.PriorityBlockingQueue;
30 | import java.util.concurrent.atomic.AtomicInteger;
31 |
32 | public class RequestQueue {
33 |
34 | /** Callback interface for completed requests. */
35 | // TODO: This should not be a generic class, because the request type can't be determined at
36 | // compile time, so all calls to onRequestFinished are unsafe. However, changing this would be
37 | // an API-breaking change. See also: https://github.com/google/volley/pull/109
38 | @Deprecated // Use RequestEventListener instead.
39 | public interface RequestFinishedListener {
40 | /** Called when a request has finished processing. */
41 | void onRequestFinished(Request request);
42 | }
43 |
44 | /** Request event types the listeners {@link RequestEventListener} will be notified about. */
45 | @Retention(RetentionPolicy.SOURCE)
46 | @IntDef({
47 | RequestEvent.REQUEST_QUEUED,
48 | RequestEvent.REQUEST_CACHE_LOOKUP_STARTED,
49 | RequestEvent.REQUEST_CACHE_LOOKUP_FINISHED,
50 | RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED,
51 | RequestEvent.REQUEST_NETWORK_DISPATCH_FINISHED,
52 | RequestEvent.REQUEST_FINISHED
53 | })
54 |
55 | public @interface RequestEvent {
56 | /** The request was added to the queue. */
57 | public static final int REQUEST_QUEUED = 0;
58 | /** Cache lookup started for the request. */
59 | public static final int REQUEST_CACHE_LOOKUP_STARTED = 1;
60 | /**
61 | * Cache lookup finished for the request and cached response is delivered or request is
62 | * queued for network dispatching.
63 | */
64 | public static final int REQUEST_CACHE_LOOKUP_FINISHED = 2;
65 | /** Network dispatch started for the request. */
66 | public static final int REQUEST_NETWORK_DISPATCH_STARTED = 3;
67 | /** The network dispatch finished for the request and response (if any) is delivered. */
68 | public static final int REQUEST_NETWORK_DISPATCH_FINISHED = 4;
69 | /**
70 | * All the work associated with the request is finished and request is removed from all the
71 | * queues.
72 | */
73 | public static final int REQUEST_FINISHED = 5;
74 | }
75 |
76 | /** Callback interface for request life cycle events. */
77 | public interface RequestEventListener {
78 | void onRequestEvent(Request> request, @RequestEvent int event);
79 | }
80 |
81 | /** Used for generating monotonically-increasing sequence numbers for requests. */
82 | private final AtomicInteger mSequenceGenerator = new AtomicInteger();
83 |
84 | /**
85 | * The set of all requests currently being processed by this RequestQueue. A Request will be in
86 | * this set if it is waiting in any queue or currently being processed by any dispatcher.
87 | */
88 | private final Set> mCurrentRequests = new HashSet<>();
89 |
90 | /** The cache triage queue. */
91 | private final PriorityBlockingQueue> mCacheQueue = new PriorityBlockingQueue<>();
92 |
93 | /** The queue of requests that are actually going out to the network. */
94 | private final PriorityBlockingQueue> mNetworkQueue = new PriorityBlockingQueue<>();
95 |
96 | /** Number of network request dispatcher threads to start. */
97 | private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4;
98 |
99 | /** Cache interface for retrieving and storing responses. */
100 | private final Cache mCache;
101 |
102 | /** Network interface for performing requests. */
103 | private final Network mNetwork;
104 |
105 | /** Response delivery mechanism. */
106 | private final ResponseDelivery mDelivery;
107 |
108 | /** The network dispatchers. */
109 | private final NetworkDispatcher[] mDispatchers;
110 |
111 | /** The cache dispatcher. */
112 | private CacheDispatcher mCacheDispatcher;
113 |
114 | private final List mFinishedListeners = new ArrayList<>();
115 |
116 | /** Collection of listeners for request life cycle events. */
117 | private final List mEventListeners = new ArrayList<>();
118 |
119 | /**
120 | * Creates the worker pool. Processing will not begin until {@link #start()} is called.
121 | *
122 | * @param cache A Cache to use for persisting responses to disk
123 | * @param network A Network interface for performing HTTP requests
124 | * @param threadPoolSize Number of network dispatcher threads to create
125 | * @param delivery A ResponseDelivery interface for posting responses and errors
126 | */
127 | public RequestQueue(
128 | Cache cache,
129 | Network network,
130 | int threadPoolSize,
131 | ResponseDelivery delivery) {
132 | mCache = cache;
133 | mNetwork = network;
134 | mDispatchers = new NetworkDispatcher[threadPoolSize];
135 | mDelivery = delivery;
136 | }
137 |
138 | /**
139 | * Creates the worker pool. Processing will not begin until {@link #start()} is called.
140 | *
141 | * @param cache A Cache to use for persisting responses to disk
142 | * @param network A Network interface for performing HTTP requests
143 | * @param threadPoolSize Number of network dispatcher threads to create
144 | */
145 | public RequestQueue(Cache cache,
146 | Network network,
147 | int threadPoolSize) {
148 | this(
149 | cache,
150 | network,
151 | threadPoolSize,
152 | new ExecutorDelivery(new Handler(Looper.getMainLooper())));
153 | }
154 |
155 | /**
156 | * Creates the worker pool. Processing will not begin until {@link #start()} is called.
157 | *
158 | * @param cache A Cache to use for persisting responses to disk
159 | * @param network A Network interface for performing HTTP requests
160 | */
161 | public RequestQueue(Cache cache, Network network) {
162 | this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
163 | }
164 |
165 | /** Starts the dispatchers in this queue. */
166 | public void start() {
167 | stop(); // Make sure any currently running dispatchers are stopped.
168 | // Create the cache dispatcher and start it.
169 | mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
170 | mCacheDispatcher.start();
171 |
172 | // Create network dispatchers (and corresponding threads) up to the pool size.
173 | for (int i = 0; i < mDispatchers.length; i++) {
174 | NetworkDispatcher networkDispatcher =
175 | new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
176 | mDispatchers[i] = networkDispatcher;
177 | networkDispatcher.start();
178 | }
179 | }
180 |
181 | /** Stops the cache and network dispatchers. */
182 | public void stop() {
183 |
184 | if (mCacheDispatcher != null) {
185 | mCacheDispatcher.quit();
186 | }
187 | for (final NetworkDispatcher mDispatcher : mDispatchers) {
188 | if (mDispatcher != null) {
189 | mDispatcher.quit();
190 | }
191 | }
192 | }
193 |
194 | /** Gets a sequence number. */
195 | public int getSequenceNumber() {
196 | return mSequenceGenerator.incrementAndGet();
197 | }
198 |
199 | /** Gets the {@link Cache} instance being used. */
200 | public Cache getCache() {
201 | return mCache;
202 | }
203 |
204 | /**
205 | * A simple predicate or filter interface for Requests, for use by {@link
206 | * RequestQueue#cancelAll(RequestFilter)}.
207 | */
208 | public interface RequestFilter {
209 | boolean apply(Request> request);
210 | }
211 |
212 | /**
213 | * Cancels all requests in this queue for which the given filter applies.
214 | *
215 | * @param filter The filtering function to use
216 | */
217 | public void cancelAll(RequestFilter filter) {
218 | synchronized (mCurrentRequests) {
219 | for (Request> request : mCurrentRequests) {
220 | if (filter.apply(request)) {
221 | request.cancel();
222 | }
223 | }
224 | }
225 | }
226 |
227 | /**
228 | * Cancels all requests in this queue with the given tag. Tag must be non-null and equality is
229 | * by identity.
230 | */
231 | public void cancelAll(final Object tag) {
232 | if (tag == null) {
233 | throw new IllegalArgumentException("Cannot cancelAll with a null tag");
234 | }
235 | cancelAll(
236 | new RequestFilter() {
237 | @Override
238 | public boolean apply(Request> request) {
239 | return request.getTag() == tag;
240 | }
241 | });
242 | }
243 |
244 | /**
245 | * Adds a Request to the dispatch queue.
246 | *
247 | * @param request The request to service
248 | * @return The passed-in request
249 | */
250 | public Request add(Request request) {
251 | // Tag the request as belonging to this queue and add it to the set of current requests.
252 | request.setRequestQueue(this);
253 | synchronized (mCurrentRequests) {
254 | mCurrentRequests.add(request);
255 | }
256 |
257 | // Process requests in the order they are added.
258 | request.setSequence(getSequenceNumber());
259 | request.addMarker("add-to-queue");
260 | sendRequestEvent(request, RequestEvent.REQUEST_QUEUED);
261 |
262 | // If the request is uncacheable, skip the cache queue and go straight to the network.
263 | if (!request.shouldCache()) {
264 | mNetworkQueue.add(request);
265 | return request;
266 | }
267 | mCacheQueue.add(request);
268 | return request;
269 | }
270 |
271 | /**
272 | * Called from {@link Request#finish(String)}, indicating that processing of the given request
273 | * has finished.
274 | */
275 | @SuppressWarnings("unchecked") // see above note on RequestFinishedListener
276 | void finish(Request request) {
277 | // Remove from the set of requests currently being processed.
278 | synchronized (mCurrentRequests) {
279 | mCurrentRequests.remove(request);
280 | }
281 | synchronized (mFinishedListeners) {
282 | for (RequestFinishedListener listener : mFinishedListeners) {
283 | listener.onRequestFinished(request);
284 | }
285 | }
286 | sendRequestEvent(request, RequestEvent.REQUEST_FINISHED);
287 | }
288 |
289 | /** Sends a request life cycle event to the listeners. */
290 | void sendRequestEvent(Request> request, @RequestEvent int event) {
291 | synchronized (mEventListeners) {
292 | for (RequestEventListener listener : mEventListeners) {
293 | listener.onRequestEvent(request, event);
294 | }
295 | }
296 | }
297 |
298 | /** Add a listener for request life cycle events. */
299 | public void addRequestEventListener(RequestEventListener listener) {
300 | synchronized (mEventListeners) {
301 | mEventListeners.add(listener);
302 | }
303 | }
304 |
305 | /** Remove a listener for request life cycle events. */
306 | public void removeRequestEventListener(RequestEventListener listener) {
307 | synchronized (mEventListeners) {
308 | mEventListeners.remove(listener);
309 | }
310 | }
311 |
312 | @Deprecated // Use RequestEventListener instead.
313 | public void addRequestFinishedListener(RequestFinishedListener listener) {
314 | synchronized (mFinishedListeners) {
315 | mFinishedListeners.add(listener);
316 | }
317 | }
318 |
319 | /** Remove a RequestFinishedListener. Has no effect if listener was not previously added. */
320 | @Deprecated // Use RequestEventListener instead.
321 | public void removeRequestFinishedListener(RequestFinishedListener listener) {
322 | synchronized (mFinishedListeners) {
323 | mFinishedListeners.remove(listener);
324 | }
325 | }
326 | }
327 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/CacheDispatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library;
18 |
19 | import android.os.Process;
20 | import androidx.annotation.VisibleForTesting;
21 |
22 | import java.util.ArrayList;
23 | import java.util.HashMap;
24 | import java.util.List;
25 | import java.util.Map;
26 | import java.util.concurrent.BlockingQueue;
27 |
28 | /**
29 | * Provides a thread for performing cache triage on a queue of requests.
30 | *
31 | *
Requests added to the specified cache queue are resolved from cache. Any deliverable response
32 | * is posted back to the caller via a {@link ResponseDelivery}. Cache misses and responses that
33 | * require refresh are enqueued on the specified network queue for processing by a {@link
34 | * NetworkDispatcher}.
35 | */
36 | public class CacheDispatcher extends Thread {
37 |
38 | private static final boolean DEBUG = VolleyLog.DEBUG;
39 |
40 | /** The queue of requests coming in for triage. */
41 | private final BlockingQueue> mCacheQueue;
42 |
43 | /** The queue of requests going out to the network. */
44 | private final BlockingQueue> mNetworkQueue;
45 |
46 | /** The cache to read from. */
47 | private final Cache mCache;
48 |
49 | /** For posting responses. */
50 | private final ResponseDelivery mDelivery;
51 |
52 | /** Used for telling us to die. */
53 | private volatile boolean mQuit = false;
54 |
55 | /** Manage list of waiting requests and de-duplicate requests with same cache key. */
56 | private final WaitingRequestManager mWaitingRequestManager;
57 |
58 | /**
59 | * Creates a new cache triage dispatcher thread. You must call {@link #start()} in order to
60 | * begin processing.
61 | *
62 | * @param cacheQueue Queue of incoming requests for triage
63 | * @param networkQueue Queue to post requests that require network to
64 | * @param cache Cache interface to use for resolution
65 | * @param delivery Delivery interface to use for posting responses
66 | */
67 | public CacheDispatcher(
68 | BlockingQueue> cacheQueue,
69 | BlockingQueue> networkQueue,
70 | Cache cache,
71 | ResponseDelivery delivery) {
72 | mCacheQueue = cacheQueue;
73 | mNetworkQueue = networkQueue;
74 | mCache = cache;
75 | mDelivery = delivery;
76 | mWaitingRequestManager = new WaitingRequestManager(this);
77 | }
78 |
79 | /**
80 | * Forces this dispatcher to quit immediately. If any requests are still in the queue, they are
81 | * not guaranteed to be processed.
82 | */
83 | public void quit() {
84 | mQuit = true;
85 | interrupt();
86 | }
87 |
88 | @Override
89 | public void run() {
90 | if (DEBUG) VolleyLog.v("start new dispatcher");
91 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
92 |
93 | // Make a blocking call to initialize the cache.
94 | mCache.initialize();
95 |
96 | while (true) {
97 | try {
98 | processRequest();
99 | } catch (InterruptedException e) {
100 | // We may have been interrupted because it was time to quit.
101 | if (mQuit) {
102 | Thread.currentThread().interrupt();
103 | return;
104 | }
105 | VolleyLog.e(
106 | "Ignoring spurious interrupt of CacheDispatcher thread; "
107 | + "use quit() to terminate it");
108 | }
109 | }
110 | }
111 |
112 | // Extracted to its own method to ensure locals have a constrained liveness scope by the GC.
113 | // This is needed to avoid keeping previous request references alive for an indeterminate amount
114 | // of time. Update consumer-proguard-rules.pro when modifying this. See also
115 | // https://github.com/google/volley/issues/114
116 | private void processRequest() throws InterruptedException {
117 | // Get a request from the cache triage queue, blocking until
118 | // at least one is available.
119 | final Request> request = mCacheQueue.take();
120 | processRequest(request);
121 | }
122 |
123 | @VisibleForTesting
124 | void processRequest(final Request> request) throws InterruptedException {
125 | request.addMarker("cache-queue-take");
126 | request.sendEvent(RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_STARTED);
127 |
128 | try {
129 | // If the request has been canceled, don't bother dispatching it.
130 | if (request.isCanceled()) {
131 | request.finish("cache-discard-canceled");
132 | return;
133 | }
134 |
135 | // Attempt to retrieve this item from cache.
136 | Cache.Entry entry = mCache.get(request.getCacheKey());
137 | if (entry == null) {
138 | request.addMarker("cache-miss");
139 | // Cache miss; send off to the network dispatcher.
140 | if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
141 | mNetworkQueue.put(request);
142 | }
143 | return;
144 | }
145 |
146 | // If it is completely expired, just send it to the network.
147 | if (entry.isExpired()) {
148 | request.addMarker("cache-hit-expired");
149 | request.setCacheEntry(entry);
150 | if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
151 | mNetworkQueue.put(request);
152 | }
153 | return;
154 | }
155 |
156 | // We have a cache hit; parse its data for delivery back to the request.
157 | request.addMarker("cache-hit");
158 | Response> response =
159 | request.parseNetworkResponse(
160 | new NetworkResponse(entry.data, entry.responseHeaders));
161 | request.addMarker("cache-hit-parsed");
162 |
163 | if (!entry.refreshNeeded()) {
164 | // Completely unexpired cache hit. Just deliver the response.
165 | mDelivery.postResponse(request, response);
166 | } else {
167 | // Soft-expired cache hit. We can deliver the cached response,
168 | // but we need to also send the request to the network for
169 | // refreshing.
170 | request.addMarker("cache-hit-refresh-needed");
171 | request.setCacheEntry(entry);
172 | // Mark the response as intermediate.
173 | response.intermediate = true;
174 |
175 | if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
176 | // Post the intermediate response back to the user and have
177 | // the delivery then forward the request along to the network.
178 | mDelivery.postResponse(
179 | request,
180 | response,
181 | new Runnable() {
182 | @Override
183 | public void run() {
184 | try {
185 | mNetworkQueue.put(request);
186 | } catch (InterruptedException e) {
187 | // Restore the interrupted status
188 | Thread.currentThread().interrupt();
189 | }
190 | }
191 | });
192 | } else {
193 | // request has been added to list of waiting requests
194 | // to receive the network response from the first request once it returns.
195 | mDelivery.postResponse(request, response);
196 | }
197 | }
198 | } finally {
199 | request.sendEvent(RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_FINISHED);
200 | }
201 | }
202 |
203 | private static class WaitingRequestManager implements Request.NetworkRequestCompleteListener {
204 |
205 | /**
206 | * Staging area for requests that already have a duplicate request in flight.
207 | *
208 | *
209 | *
containsKey(cacheKey) indicates that there is a request in flight for the given
210 | * cache key.
211 | *
get(cacheKey) returns waiting requests for the given cache key. The in flight
212 | * request is not contained in that list. Is null if no requests are staged.
213 | *
214 | */
215 | private final Map>> mWaitingRequests = new HashMap<>();
216 |
217 | private final CacheDispatcher mCacheDispatcher;
218 |
219 | WaitingRequestManager(CacheDispatcher cacheDispatcher) {
220 | mCacheDispatcher = cacheDispatcher;
221 | }
222 |
223 | /** Request received a valid response that can be used by other waiting requests. */
224 | @Override
225 | public void onResponseReceived(Request> request, Response> response) {
226 | if (response.cacheEntry == null || response.cacheEntry.isExpired()) {
227 | onNoUsableResponseReceived(request);
228 | return;
229 | }
230 | String cacheKey = request.getCacheKey();
231 | List> waitingRequests;
232 | synchronized (this) {
233 | waitingRequests = mWaitingRequests.remove(cacheKey);
234 | }
235 | if (waitingRequests != null) {
236 | if (VolleyLog.DEBUG) {
237 | VolleyLog.v(
238 | "Releasing %d waiting requests for cacheKey=%s.",
239 | waitingRequests.size(), cacheKey);
240 | }
241 | // Process all queued up requests.
242 | for (Request> waiting : waitingRequests) {
243 | mCacheDispatcher.mDelivery.postResponse(waiting, response);
244 | }
245 | }
246 | }
247 |
248 | /** No valid response received from network, release waiting requests. */
249 | @Override
250 | public synchronized void onNoUsableResponseReceived(Request> request) {
251 | String cacheKey = request.getCacheKey();
252 | List> waitingRequests = mWaitingRequests.remove(cacheKey);
253 | if (waitingRequests != null && !waitingRequests.isEmpty()) {
254 | if (VolleyLog.DEBUG) {
255 | VolleyLog.v(
256 | "%d waiting requests for cacheKey=%s; resend to network",
257 | waitingRequests.size(), cacheKey);
258 | }
259 | Request> nextInLine = waitingRequests.remove(0);
260 | mWaitingRequests.put(cacheKey, waitingRequests);
261 | nextInLine.setNetworkRequestCompleteListener(this);
262 | try {
263 | mCacheDispatcher.mNetworkQueue.put(nextInLine);
264 | } catch (InterruptedException iex) {
265 | VolleyLog.e("Couldn't add request to queue. %s", iex.toString());
266 | // Restore the interrupted status of the calling thread (i.e. NetworkDispatcher)
267 | Thread.currentThread().interrupt();
268 | // Quit the current CacheDispatcher thread.
269 | mCacheDispatcher.quit();
270 | }
271 | }
272 | }
273 |
274 | /**
275 | * For cacheable requests, if a request for the same cache key is already in flight, add it
276 | * to a queue to wait for that in-flight request to finish.
277 | *
278 | * @return whether the request was queued. If false, we should continue issuing the request
279 | * over the network. If true, we should put the request on hold to be processed when the
280 | * in-flight request finishes.
281 | */
282 | private synchronized boolean maybeAddToWaitingRequests(Request> request) {
283 | String cacheKey = request.getCacheKey();
284 | // Insert request into stage if there's already a request with the same cache key
285 | // in flight.
286 | if (mWaitingRequests.containsKey(cacheKey)) {
287 | // There is already a request in flight. Queue up.
288 | List> stagedRequests = mWaitingRequests.get(cacheKey);
289 | if (stagedRequests == null) {
290 | stagedRequests = new ArrayList<>();
291 | }
292 | request.addMarker("waiting-for-response");
293 | stagedRequests.add(request);
294 | mWaitingRequests.put(cacheKey, stagedRequests);
295 | if (VolleyLog.DEBUG) {
296 | VolleyLog.d("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
297 | }
298 | return true;
299 | } else {
300 | // Insert 'null' queue for this cacheKey, indicating there is now a request in
301 | // flight.
302 | mWaitingRequests.put(cacheKey, null);
303 | request.setNetworkRequestCompleteListener(this);
304 | if (VolleyLog.DEBUG) {
305 | VolleyLog.d("new request, sending to network %s", cacheKey);
306 | }
307 | return false;
308 | }
309 | }
310 | }
311 | }
312 |
--------------------------------------------------------------------------------
/library/src/main/java/com/remoteconfig/library/toolbox/BasicNetwork.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 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.remoteconfig.library.toolbox;
18 |
19 | import android.os.SystemClock;
20 | import com.remoteconfig.library.AuthFailureError;
21 | import com.remoteconfig.library.Cache.Entry;
22 | import com.remoteconfig.library.Header;
23 | import com.remoteconfig.library.Network;
24 | import com.remoteconfig.library.NetworkResponse;
25 | import com.remoteconfig.library.RemoteError;
26 | import com.remoteconfig.library.Request;
27 | import com.remoteconfig.library.RetryPolicy;
28 | import com.remoteconfig.library.VolleyLog;
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.net.HttpURLConnection;
33 | import java.net.MalformedURLException;
34 | import java.net.SocketTimeoutException;
35 | import java.util.ArrayList;
36 | import java.util.Collections;
37 | import java.util.HashMap;
38 | import java.util.List;
39 | import java.util.Map;
40 | import java.util.Set;
41 | import java.util.TreeMap;
42 | import java.util.TreeSet;
43 |
44 | /** A network performing FetchRemote requests over an {@link HttpStack}. */
45 | @SuppressWarnings("deprecation")
46 | public class BasicNetwork implements Network {
47 | protected static final boolean DEBUG = VolleyLog.DEBUG;
48 |
49 | private static final int SLOW_REQUEST_THRESHOLD_MS = 3000;
50 |
51 | private static final int DEFAULT_POOL_SIZE = 4096;
52 |
53 | /**
54 | * @deprecated Should never have been exposed in the API. This field may be removed in a future
55 | * release of FetchRemote.
56 | */
57 | @SuppressWarnings("DeprecatedIsStillUsed")
58 | @Deprecated protected final HttpStack mHttpStack;
59 |
60 | private final BaseHttpStack mBaseHttpStack;
61 |
62 | protected final ByteArrayPool mPool;
63 |
64 | /**
65 | * @param httpStack HTTP stack to be used
66 | * @deprecated use {@link #BasicNetwork(BaseHttpStack)} instead to avoid depending on Apache
67 | * HTTP. This method may be removed in a future release of FetchRemote.
68 | */
69 |
70 | @SuppressWarnings("DeprecatedIsStillUsed")
71 | @Deprecated
72 | public BasicNetwork(HttpStack httpStack) {
73 | // If a pool isn't passed in, then build a small default pool that will give us a lot of
74 | // benefit and not use too much memory.
75 | this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
76 | }
77 |
78 | /**
79 | * @param httpStack HTTP stack to be used
80 | * @param pool a buffer pool that improves GC performance in copy operations
81 | * @deprecated use {@link #BasicNetwork(BaseHttpStack, ByteArrayPool)} instead to avoid
82 | * depending on Apache HTTP. This method may be removed in a future release of FetchRemote.
83 | */
84 | @SuppressWarnings("DeprecatedIsStillUsed")
85 | @Deprecated
86 | public BasicNetwork(HttpStack httpStack, ByteArrayPool pool) {
87 | mHttpStack = httpStack;
88 | mBaseHttpStack = new AdaptedHttpStack(httpStack);
89 | mPool = pool;
90 | }
91 |
92 | /** @param httpStack HTTP stack to be used */
93 | public BasicNetwork(BaseHttpStack httpStack) {
94 | // If a pool isn't passed in, then build a small default pool that will give us a lot of
95 | // benefit and not use too much memory.
96 | this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
97 | }
98 |
99 | /**
100 | * @param httpStack HTTP stack to be used
101 | * @param pool a buffer pool that improves GC performance in copy operations
102 | */
103 | @SuppressWarnings("DeprecatedIsStillUsed")
104 | public BasicNetwork(BaseHttpStack httpStack, ByteArrayPool pool) {
105 | mBaseHttpStack = httpStack;
106 | // Populate mHttpStack for backwards compatibility, since it is a protected field. However,
107 | // we won't use it directly here, so clients which don't access it directly won't need to
108 | // depend on Apache HTTP.
109 | mHttpStack = httpStack;
110 | mPool = pool;
111 | }
112 |
113 | //performRequest
114 | @Override
115 | public NetworkResponse performRequest(Request> request) throws RemoteError {
116 | long requestStart = SystemClock.elapsedRealtime();
117 | while (true) {
118 | HttpResponse httpResponse = null;
119 | byte[] responseContents = null;
120 | List responseHeaders = Collections.emptyList();
121 | try {
122 | // Gather headers.
123 | Map additionalRequestHeaders =
124 | getCacheHeaders(request.getCacheEntry());
125 | httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);
126 | int statusCode = httpResponse.getStatusCode();
127 |
128 | responseHeaders = httpResponse.getHeaders();
129 | // Handle cache validation.
130 | if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
131 | Entry entry = request.getCacheEntry();
132 | if (entry == null) {
133 | return new NetworkResponse(
134 | HttpURLConnection.HTTP_NOT_MODIFIED,
135 | /* data= */ null,
136 | /* notModified= */ true,
137 | SystemClock.elapsedRealtime() - requestStart,
138 | responseHeaders);
139 | }
140 | // Combine cached and response headers so the response will be complete.
141 | List combinedHeaders = combineHeaders(responseHeaders, entry);
142 | return new NetworkResponse(
143 | HttpURLConnection.HTTP_NOT_MODIFIED,
144 | entry.data,
145 | /* notModified= */ true,
146 | SystemClock.elapsedRealtime() - requestStart,
147 | combinedHeaders);
148 | }
149 |
150 | // Some responses such as 204s do not have content. We must check.
151 | InputStream inputStream = httpResponse.getContent();
152 | if (inputStream != null) {
153 | responseContents =
154 | inputStreamToBytes(inputStream, httpResponse.getContentLength());
155 | } else {
156 | // Add 0 byte response as a way of honestly representing a
157 | // no-content request.
158 | responseContents = new byte[0];
159 | }
160 |
161 | // if the request is slow, log it.
162 | long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
163 | logSlowRequests(requestLifetime, request, responseContents, statusCode);
164 |
165 | if (statusCode < 200 || statusCode > 299) {
166 | throw new IOException();
167 | }
168 | return new NetworkResponse(
169 | statusCode,
170 | responseContents,
171 | /* notModified= */ false,
172 | SystemClock.elapsedRealtime() - requestStart,
173 | responseHeaders);
174 | } catch (SocketTimeoutException e) {
175 | attemptRetryOnException("socket", request, new RemoteError());
176 | } catch (MalformedURLException e) {
177 | throw new RuntimeException("Bad URL " + request.getUrl(), e);
178 | } catch (IOException e) {
179 | int statusCode;
180 | if (httpResponse != null) {
181 | statusCode = httpResponse.getStatusCode();
182 | } else {
183 | throw new RemoteError(e);
184 | }
185 | VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
186 | NetworkResponse networkResponse;
187 | if (responseContents != null) {
188 | networkResponse =
189 | new NetworkResponse(
190 | statusCode,
191 | responseContents,
192 | /* notModified= */ false,
193 | SystemClock.elapsedRealtime() - requestStart,
194 | responseHeaders);
195 | if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED
196 | || statusCode == HttpURLConnection.HTTP_FORBIDDEN) {
197 | attemptRetryOnException(
198 | "auth", request, new AuthFailureError(networkResponse));
199 | } else if (statusCode >= 400 && statusCode <= 499) {
200 | // Don't retry other client errors.
201 | throw new RemoteError(networkResponse);
202 | } else if (statusCode >= 500 && statusCode <= 599) {
203 | if (request.shouldRetryServerErrors()) {
204 | attemptRetryOnException(
205 | "server", request, new RemoteError(networkResponse));
206 | } else {
207 | throw new RemoteError(networkResponse);
208 | }
209 | } else {
210 | // 3xx? No reason to retry.
211 | throw new RemoteError(networkResponse);
212 | }
213 | } else {
214 | attemptRetryOnException("network", request, new RemoteError());
215 | }
216 | }
217 | }
218 | }
219 |
220 | /** Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete. */
221 | private void logSlowRequests(
222 | long requestLifetime, Request> request, byte[] responseContents, int statusCode) {
223 | if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
224 | VolleyLog.d(
225 | "HTTP response for request=<%s> [lifetime=%d], [size=%s], "
226 | + "[rc=%d], [retryCount=%s]",
227 | request,
228 | requestLifetime,
229 | responseContents != null ? responseContents.length : "null",
230 | statusCode,
231 | request.getRetryPolicy().getCurrentRetryCount());
232 | }
233 | }
234 |
235 | /**
236 | * Attempts to prepare the request for a retry. If there are no more attempts remaining in the
237 | * request's retry policy, a timeout exception is thrown.
238 | *
239 | * @param request The request to use.
240 | */
241 | private static void attemptRetryOnException(
242 | String logPrefix, Request> request, RemoteError exception) throws RemoteError {
243 | RetryPolicy retryPolicy = request.getRetryPolicy();
244 | int oldTimeout = request.getTimeoutMs();
245 |
246 | try {
247 | retryPolicy.retry(exception);
248 | } catch (RemoteError e) {
249 | request.addMarker(
250 | String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout));
251 | throw e;
252 | }
253 | request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout));
254 | }
255 |
256 | private Map getCacheHeaders(Entry entry) {
257 | // If there's no cache entry, we're done.
258 | if (entry == null) {
259 | return Collections.emptyMap();
260 | }
261 |
262 | Map headers = new HashMap<>();
263 |
264 | if (entry.etag != null) {
265 | headers.put("If-None-Match", entry.etag);
266 | }
267 |
268 | if (entry.lastModified > 0) {
269 | headers.put(
270 | "If-Modified-Since", HttpHeaderParser.formatEpochAsRfc1123(entry.lastModified));
271 | }
272 |
273 | return headers;
274 | }
275 |
276 | protected void logError(String what, String url, long start) {
277 | long now = SystemClock.elapsedRealtime();
278 | VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
279 | }
280 |
281 | /** Reads the contents of an InputStream into a byte[]. */
282 | private byte[] inputStreamToBytes(InputStream in, int contentLength)
283 | throws IOException, RemoteError {
284 | PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, contentLength);
285 | byte[] buffer = null;
286 | try {
287 | if (in == null) {
288 | throw new RemoteError();
289 | }
290 | buffer = mPool.getBuf(1024);
291 | int count;
292 | while ((count = in.read(buffer)) != -1) {
293 | bytes.write(buffer, 0, count);
294 | }
295 | return bytes.toByteArray();
296 | } finally {
297 | try {
298 | // Close the InputStream and release the resources by "consuming the content".
299 | if (in != null) {
300 | in.close();
301 | }
302 | } catch (IOException e) {
303 | // This can happen if there was an exception above that left the stream in
304 | // an invalid state.
305 | VolleyLog.v("Error occurred when closing InputStream");
306 | }
307 | mPool.returnBuf(buffer);
308 | bytes.close();
309 | }
310 | }
311 |
312 | /**
313 | * Converts Headers[] to Map<String, String>.
314 | *
315 | * @deprecated Should never have been exposed in the API. This method may be removed in a future
316 | * release of FetchRemote.
317 | */
318 | @Deprecated
319 | protected static Map convertHeaders(Header[] headers) {
320 | Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
321 | for (int i = 0; i < headers.length; i++) {
322 | result.put(headers[i].getName(), headers[i].getValue());
323 | }
324 | return result;
325 | }
326 |
327 | /**
328 | * Combine cache headers with network response headers for an HTTP 304 response.
329 | *
330 | *
An HTTP 304 response does not have all header fields. We have to use the header fields
331 | * from the cache entry plus the new ones from the response. See also:
332 | * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
333 | *
334 | * @param responseHeaders Headers from the network response.
335 | * @param entry The cached response.
336 | * @return The combined list of headers.
337 | */
338 | private static List combineHeaders(List responseHeaders, Entry entry) {
339 | // First, create a case-insensitive set of header names from the network
340 | // response.
341 | Set headerNamesFromNetworkResponse = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
342 | if (!responseHeaders.isEmpty()) {
343 | for (Header header : responseHeaders) {
344 | headerNamesFromNetworkResponse.add(header.getName());
345 | }
346 | }
347 |
348 | // Second, add headers from the cache entry to the network response as long as
349 | // they didn't appear in the network response, which should take precedence.
350 | List combinedHeaders = new ArrayList<>(responseHeaders);
351 | if (entry.allResponseHeaders != null) {
352 | if (!entry.allResponseHeaders.isEmpty()) {
353 | for (Header header : entry.allResponseHeaders) {
354 | if (!headerNamesFromNetworkResponse.contains(header.getName())) {
355 | combinedHeaders.add(header);
356 | }
357 | }
358 | }
359 | } else {
360 | // Legacy caches only have entry.responseHeaders.
361 | if (!entry.responseHeaders.isEmpty()) {
362 | for (Map.Entry header : entry.responseHeaders.entrySet()) {
363 | if (!headerNamesFromNetworkResponse.contains(header.getKey())) {
364 | combinedHeaders.add(new Header(header.getKey(), header.getValue()));
365 | }
366 | }
367 | }
368 | }
369 | return combinedHeaders;
370 | }
371 | }
372 |
--------------------------------------------------------------------------------