VancedMicroG/src/org/microg/gms/auth/AuthResponse.java

103 lines
3.4 KiB
Java
Raw Normal View History

2015-02-01 22:27:46 +00:00
/*
* Copyright 2013-2015 µg Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.microg.gms.auth;
import android.util.Log;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AuthResponse {
private static final String TAG = "GmsAuthResponse";
@ResponseField("SID")
public String Sid;
@ResponseField("LSID")
public String LSid;
@ResponseField("Auth")
public String auth;
@ResponseField("Token")
public String token;
@ResponseField("Email")
public String email;
@ResponseField("services")
2015-02-03 22:08:55 +00:00
public String services;
2015-02-01 22:27:46 +00:00
@ResponseField("GooglePlusUpgrade")
public boolean isGooglePlusUpgrade;
@ResponseField("PicasaUser")
public String picasaUserName;
@ResponseField("RopText")
public String ropText;
@ResponseField("RopRevision")
public int ropRevision;
@ResponseField("firstName")
public String firstName;
@ResponseField("lastName")
public String lastName;
@ResponseField("issueAdvice")
public String issueAdvice;
2015-02-03 22:08:55 +00:00
@ResponseField("accountId")
public String accountId;
@ResponseField("Expiry")
public long expiry = -1;
@ResponseField("storeConsentRemotely")
public boolean storeConsentRemotely;
2015-02-01 22:27:46 +00:00
public static AuthResponse parse(String result) {
AuthResponse response = new AuthResponse();
String[] entries = result.split("\n");
for (String s : entries) {
2015-02-03 22:08:55 +00:00
String[] keyValuePair = s.split("=", 2);
2015-02-01 22:27:46 +00:00
String key = keyValuePair[0].trim();
String value = keyValuePair[1].trim();
try {
for (Field field : AuthResponse.class.getDeclaredFields()) {
if (field.isAnnotationPresent(ResponseField.class) &&
key.equals(field.getAnnotation(ResponseField.class).value())) {
if (field.getType().equals(String.class)) {
field.set(response, value);
} else if (field.getType().equals(boolean.class)) {
field.setBoolean(response, value.equals("1"));
2015-02-03 22:08:55 +00:00
} else if (field.getType().equals(long.class)) {
field.setLong(response, Long.parseLong(value));
2015-02-01 22:27:46 +00:00
} else if (field.getType().equals(int.class)) {
field.setInt(response, Integer.parseInt(value));
}
}
}
} catch (Exception e) {
Log.w(TAG, e);
}
}
return response;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
private @interface ResponseField {
public String value();
}
}