Commit 9f80a6ed authored by Administrator's avatar Administrator

update

parent 8e95a880
......@@ -49,7 +49,10 @@ dependencies {
//compile 'com.android.support:support-v4:24.2.0'
api project(':echo-lib')
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
//引入netty之后,class直接就爆炸了。考虑实现API精简
implementation 'com.android.support:multidex:1.0.3'
......
package com.virjar.echo.adr.repo;
import android.text.TextUtils;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class APIServices {
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EchoConfig.getApiURL())
.callFactory(createInterceptedOkHttp())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
public static UserApi userApi = retrofit.create(UserApi.class);
private static OkHttpClient createInterceptedOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String token = EchoConfig.getToken();
if (TextUtils.isEmpty(token)) {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
//自动增加登录token
Request originalRequest = chain.request();
Request updateRequest = originalRequest.newBuilder().header("Token", token).build();
return chain.proceed(updateRequest);
}
});
return builder.build();
}
}
......@@ -16,6 +16,11 @@ public class EchoConfig {
private static String defaultServerHost;
private static int defaultServerPort;
private static final String defaultApiURL = "http://echo.virjar.com/";
private static final String loginTokenKey = "loginToken";
private static final String userNameKey = "userName";
private static final String userPasswordKey = "password";
public static void init(Context context) {
sharedPreferences = context.getSharedPreferences(configFile, Context.MODE_PRIVATE);
......@@ -23,6 +28,7 @@ public class EchoConfig {
defaultServerPort = Integer.parseInt(context.getString(R.string.default_server_port));
getClientId();
getApiURL();
}
public static String getEchoServer() {
......@@ -43,4 +49,42 @@ public class EchoConfig {
}
return getClientId();
}
public static String getApiURL() {
String serverApiUrl = sharedPreferences.getString("server_api_url", "");
if (TextUtils.isEmpty(serverApiUrl)) {
serverApiUrl = defaultApiURL;
sharedPreferences.edit().putString("server_api_url", serverApiUrl).apply();
}
return serverApiUrl;
}
public static void updateLoginToken(String token) {
if (TextUtils.isEmpty(token)) {
return;
}
sharedPreferences.edit().putString(loginTokenKey, token).apply();
}
public static String getToken() {
return sharedPreferences.getString(loginTokenKey, "");
}
public static void saveUsernameAndPassword(String userName, String password) {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(userNameKey, userName);
edit.putString(userPasswordKey, password);
edit.apply();
}
public static String getUsername() {
return sharedPreferences.getString(userNameKey, "");
}
public static String getPassword() {
return sharedPreferences.getString(userPasswordKey, "");
}
}
package com.virjar.echo.adr.repo;
import retrofit2.Call;
import retrofit2.http.GET;
public interface UserApi {
@GET("api/user/userInfo")
Call<?> loginWithToken();
}
......@@ -41,6 +41,26 @@ public class LoginActivity extends AppCompatActivity {
});
}
private void autoLoginWithToken() {
// String token = EchoConfig.getToken();
// if (TextUtils.isEmpty(token)) {
// autoLoginWithUserName();
// return;
// }
// return new Request.Builder()
// .get()
// .url(url)
// .addHeader("Connection","close")
// .build();
//
// HttpClient.getClient().newCall()
}
private void autoLoginWithUserName() {
}
private void setupAnimation() {
imageView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
public void onSwipeTop() {
......
package com.virjar.echo.adr.util;
public class Constants {
public static final String url = "";
}
package com.virjar.echo.adr.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* some utils migrated from guava
*/
public class Lists {
public static <E> ArrayList<E> newArrayList(E... elements) {
// Avoid integer overflow when a large array is passed in
int capacity = computeArrayListCapacity(elements.length);
ArrayList<E> list = new ArrayList<E>(capacity);
Collections.addAll(list, elements);
return list;
}
static int computeArrayListCapacity(int arraySize) {
return saturatedCast(5L + arraySize + (arraySize / 10));
}
/**
* Returns the {@code int} nearest in value to {@code value}.
*
* @param value any {@code long} value
* @return the same value cast to {@code int} if it is in the range of the
* {@code int} type, {@link Integer#MAX_VALUE} if it is too large,
* or {@link Integer#MIN_VALUE} if it is too small
*/
public static int saturatedCast(long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (value < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) value;
}
public static <E> LinkedList<E> newLinkedList() {
return new LinkedList<E>();
}
static <T> Collection<T> cast(Iterable<T> iterable) {
return (Collection<T>) iterable;
}
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(
Iterable<? extends E> elements) {
// We copy elements to an ArrayList first, rather than incurring the
// quadratic cost of adding them to the COWAL directly.
Collection<? extends E> elementsCollection =
(elements instanceof Collection)
? cast(elements)
: newArrayList(elements);
return new CopyOnWriteArrayList<E>(elementsCollection);
}
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
// Let ArrayList's sizing logic work, if possible
return (elements instanceof Collection)
? new ArrayList<E>(cast(elements))
: newArrayList(elements.iterator());
}
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) {
ArrayList<E> list = newArrayList();
addAll(list, elements);
return list;
}
public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
boolean wasModified = false;
while (iterator.hasNext()) {
wasModified |= addTo.add(iterator.next());
}
return wasModified;
}
public static <T> List<T> newArrayListWithCapacity(int length) {
return new ArrayList<>(length);
}
}
......@@ -13,6 +13,8 @@
<string name="server_port_title">ServerPort</string>
<string name="default_server_port">5698</string>
<string name="server_api_url">API Url</string>
<string name="client_id_title">ClientId</string>
<string name="reply_title">Default reply action</string>
......
......@@ -14,6 +14,12 @@
app:title="@string/server_port_title"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
app:key="server_api_url"
app:title="@string/server_api_url"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/client_header">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment