Permalink
Browse files

Include new query parameters for device type.

Fix bug in sample where the picture was downloaded on the ui thread.
Add new query parameter logic for upload requests.
Update error messages from server.
  • Loading branch information...
1 parent b0bcd8d commit 0984e134d214d8b1fde5966624c106f9ab7e79b9 skrueger committed Dec 13, 2012
@@ -4,7 +4,7 @@
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
-import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
@@ -29,6 +29,21 @@
public class ViewProfileActivity extends Activity {
+ private class DownloadProfilePictureAsyncTask extends AsyncTask<LiveDownloadOperation, Void, BitmapDrawable> {
+ @Override
+ protected BitmapDrawable doInBackground(LiveDownloadOperation... params) {
+ return new BitmapDrawable(getResources(), params[0].getStream());
+ }
+
+ @Override
+ protected void onPostExecute(BitmapDrawable profilePicture) {
+ mNameTextView.setCompoundDrawablesWithIntrinsicBounds(profilePicture,
+ null,
+ null,
+ null);
+ }
+ }
+
private TextView mNameTextView;
@Override
@@ -121,11 +136,9 @@ public void onDownloadFailed(LiveOperationException exception,
@Override
public void onDownloadCompleted(LiveDownloadOperation operation) {
- Drawable profilePicture = new BitmapDrawable(operation.getStream());
- mNameTextView.setCompoundDrawablesWithIntrinsicBounds(profilePicture,
- null,
- null,
- null);
+ DownloadProfilePictureAsyncTask task =
+ new DownloadProfilePictureAsyncTask();
+ task.execute(operation);
}
});
}
View
@@ -3,5 +3,5 @@
package="com.microsoft.live"
android:versionCode="1"
android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
+ <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>
</manifest>
@@ -22,7 +22,6 @@
import org.json.JSONObject;
import android.net.Uri;
-import android.net.Uri.Builder;
import android.os.Build;
import android.text.TextUtils;
@@ -41,39 +40,55 @@
public enum Redirects {
SUPPRESS {
@Override
- protected void appendQueryParameter(Builder builder) {
- appendSuppressRedirects(builder, Boolean.TRUE);
+ protected void setQueryParameterOn(UriBuilder builder) {
+ Redirects.setQueryParameterOn(builder, Boolean.TRUE);
}
}, UNSUPPRESSED {
@Override
- protected void appendQueryParameter(Builder builder) {
- appendSuppressRedirects(builder, Boolean.FALSE);
+ protected void setQueryParameterOn(UriBuilder builder) {
+ Redirects.setQueryParameterOn(builder, Boolean.FALSE);
}
};
- protected abstract void appendQueryParameter(Uri.Builder builder);
-
- private static void appendSuppressRedirects(Uri.Builder builder, Boolean value) {
+ /**
+ * Sets the suppress_redirects query parameter by removing all existing ones
+ * and then appending it on the given UriBuilder.
+ */
+ protected abstract void setQueryParameterOn(UriBuilder builder);
+
+ private static void setQueryParameterOn(UriBuilder builder, Boolean value) {
+ // The Live SDK is designed to use our value of suppress_redirects.
+ // If it uses any other value it could cause issues. Remove any previously
+ // existing suppress_redirects and use ours.
+ builder.removeQueryParametersWithKey(QueryParameters.SUPPRESS_REDIRECTS);
builder.appendQueryParameter(QueryParameters.SUPPRESS_REDIRECTS, value.toString());
}
}
public enum ResponseCodes {
SUPPRESS {
@Override
- protected void appendQueryParameter(Builder builder) {
- appendSuppressResponseCodes(builder, Boolean.TRUE);
+ protected void setQueryParameterOn(UriBuilder builder) {
+ ResponseCodes.setQueryParameterOn(builder, Boolean.TRUE);
}
}, UNSUPPRESSED {
@Override
- protected void appendQueryParameter(Builder builder) {
- appendSuppressResponseCodes(builder, Boolean.FALSE);
+ protected void setQueryParameterOn(UriBuilder builder) {
+ ResponseCodes.setQueryParameterOn(builder, Boolean.FALSE);
}
};
- protected abstract void appendQueryParameter(Uri.Builder builder);
-
- private static void appendSuppressResponseCodes(Uri.Builder builder, Boolean value) {
+ /**
+ * Sets the suppress_response_codes query parameter by removing all existing ones
+ * and then appending it on the given UriBuilder.
+ */
+ protected abstract void setQueryParameterOn(UriBuilder builder);
+
+ private static void setQueryParameterOn(UriBuilder builder, Boolean value) {
+ // The Live SDK is designed to use our value of suppress_response_codes.
+ // If it uses any other value it could cause issues. Remove any previously
+ // existing suppress_response_codes and use ours.
+ builder.removeQueryParametersWithKey(QueryParameters.SUPPRESS_RESPONSE_CODES);
builder.appendQueryParameter(QueryParameters.SUPPRESS_RESPONSE_CODES, value.toString());
}
}
@@ -105,7 +120,11 @@ private static Header createAuthroizationHeader(LiveConnectSession session) {
private final String path;
private final ResponseHandler<ResponseType> responseHandler;
private final LiveConnectSession session;
- protected final Uri.Builder requestUri;
+
+ protected final UriBuilder requestUri;
+
+ /** The original path string parsed into a Uri object. */
+ protected final Uri pathUri;
public ApiRequest(LiveConnectSession session,
HttpClient client,
@@ -139,36 +158,22 @@ public ApiRequest(LiveConnectSession session,
this.responseHandler = responseHandler;
this.path = path;
- Uri.Builder builder;
- Uri pathUri = Uri.parse(path);
- if (pathUri.isAbsolute()) {
- builder = pathUri.buildUpon();
- } else {
- builder = Config.INSTANCE.getApiUri()
- .buildUpon()
- .appendEncodedPath(pathUri.getPath())
- .encodedQuery(pathUri.getQuery());
- }
+ UriBuilder builder;
+ this.pathUri = Uri.parse(path);
- // we need to see if we have any extra query params
- // that need to be added on to the request uri
- String apiQuery = Config.INSTANCE.getApiUri().getEncodedQuery();
- String pathQuery = builder.build().getEncodedQuery();
- String query;
- if (apiQuery != null && pathQuery != null) {
- query = TextUtils.join("&", new String[]{apiQuery, pathQuery});
- } else if (apiQuery != null) {
- query = apiQuery;
- } else if (pathQuery != null) {
- query = pathQuery;
+ if (this.pathUri.isAbsolute()) {
+ // if the path is absolute we will just use that entire path
+ builder = UriBuilder.newInstance(this.pathUri);
} else {
- query = "";
+ // if it is a relative path then we should use the config's API URI,
+ // which is usually something like https://apis.live.net/v5.0
+ builder = UriBuilder.newInstance(Config.INSTANCE.getApiUri())
+ .appendToPath(this.pathUri.getEncodedPath())
+ .query(this.pathUri.getQuery());
}
- builder.encodedQuery(query);
-
- responseCodes.appendQueryParameter(builder);
- redirects.appendQueryParameter(builder);
+ responseCodes.setQueryParameterOn(builder);
+ redirects.setQueryParameterOn(builder);
this.requestUri = builder;
}
@@ -311,9 +311,8 @@ public void addObserver(OAuthRequestObserver observer) {
* passed in listener when it is completed.
*/
public void execute() {
- String displayType = OAuth.DisplayType.TOUCH.toString().toLowerCase();
+ String displayType = this.getDisplayParameter();
String responseType = OAuth.ResponseType.CODE.toString().toLowerCase();
- String theme = OAuth.ThemeType.DEFAULT.toString().toLowerCase();
String locale = Locale.getDefault().toString();
Uri requestUri = Config.INSTANCE.getOAuthAuthorizeUri()
.buildUpon()
@@ -322,7 +321,6 @@ public void execute() {
.appendQueryParameter(OAuth.DISPLAY, displayType)
.appendQueryParameter(OAuth.RESPONSE_TYPE, responseType)
.appendQueryParameter(OAuth.LOCALE, locale)
- .appendQueryParameter(OAuth.THEME, theme)
.appendQueryParameter(OAuth.REDIRECT_URI, this.redirectUri)
.build();
@@ -346,6 +344,17 @@ public boolean removeObserver(OAuthRequestObserver observer) {
}
/**
+ * Gets the display parameter by looking at the screen size of the activity.
+ * @return "android_phone" for phones and "android_tablet" for tablets.
+ */
+ private String getDisplayParameter() {
+ ScreenSize screenSize = ScreenSize.determineScreenSize(this.activity);
+ DeviceType deviceType = screenSize.getDeviceType();
+
+ return deviceType.getDisplayParameter().toString().toLowerCase();
+ }
+
+ /**
* Called when the response uri contains an access_token in the fragment.
*
* This method reads the response and calls back the LiveOAuthListener on the UI/main thread,
@@ -0,0 +1,31 @@
+//------------------------------------------------------------------------------
+// Copyright (c) 2012 Microsoft Corporation. All rights reserved.
+//
+// Description: See the class level JavaDoc comments.
+//------------------------------------------------------------------------------
+
+package com.microsoft.live;
+
+import com.microsoft.live.OAuth.DisplayType;
+
+/**
+ * The type of the device is used to determine the display query parameter for login.live.com.
+ * Phones have a display parameter of android_phone.
+ * Tablets have a display parameter of android_tablet.
+ */
+enum DeviceType {
+ PHONE {
+ @Override
+ public DisplayType getDisplayParameter() {
+ return DisplayType.ANDROID_PHONE;
+ }
+ },
+ TABLET {
+ @Override
+ public DisplayType getDisplayParameter() {
+ return DisplayType.ANDROID_TABLET;
+ }
+ };
+
+ abstract public DisplayType getDisplayParameter();
+}
@@ -16,10 +16,8 @@
final class OAuth {
public enum DisplayType {
- NONE,
- PAGE,
- POPUP,
- TOUCH;
+ ANDROID_PHONE,
+ ANDROID_TABLET
}
public enum ErrorType {
@@ -86,15 +84,8 @@
TOKEN;
}
- public enum ThemeType {
- DARK,
- LIGHT,
- DEFAULT;
- }
-
public enum TokenType {
- BEARER,
- MAC;
+ BEARER
}
/**
@@ -0,0 +1,73 @@
+//------------------------------------------------------------------------------
+// Copyright (c) 2012 Microsoft Corporation. All rights reserved.
+//
+// Description: See the class level JavaDoc comments.
+//------------------------------------------------------------------------------
+
+package com.microsoft.live;
+
+import android.app.Activity;
+import android.content.res.Configuration;
+import android.util.Log;
+
+/**
+ * The ScreenSize is used to determine the DeviceType.
+ * Small and Normal ScreenSizes are Phones.
+ * Large and XLarge are Tablets.
+ */
+enum ScreenSize {
+ SMALL {
+ @Override
+ public DeviceType getDeviceType() {
+ return DeviceType.PHONE;
+ }
+ },
+ NORMAL {
+ @Override
+ public DeviceType getDeviceType() {
+ return DeviceType.PHONE;
+ }
+
+ },
+ LARGE {
+ @Override
+ public DeviceType getDeviceType() {
+ return DeviceType.TABLET;
+ }
+ },
+ XLARGE {
+ @Override
+ public DeviceType getDeviceType() {
+ return DeviceType.TABLET;
+ }
+ };
+
+ public abstract DeviceType getDeviceType();
+
+ /**
+ * Configuration.SCREENLAYOUT_SIZE_XLARGE was not provided in API level 9.
+ * However, its value of 4 does show up.
+ */
+ private static final int SCREENLAYOUT_SIZE_XLARGE = 4;
+
+ public static ScreenSize determineScreenSize(Activity activity) {
+ int screenLayout = activity.getResources().getConfiguration().screenLayout;
+ int screenLayoutMasked = screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
+ switch (screenLayoutMasked) {
+ case Configuration.SCREENLAYOUT_SIZE_SMALL:
+ return SMALL;
+ case Configuration.SCREENLAYOUT_SIZE_NORMAL:
+ return NORMAL;
+ case Configuration.SCREENLAYOUT_SIZE_LARGE:
+ return LARGE;
+ case SCREENLAYOUT_SIZE_XLARGE:
+ return XLARGE;
+ default:
+ // If we cannot determine the ScreenSize, we'll guess and say it's normal.
+ Log.d(
+ "Live SDK ScreenSize",
+ "Unable to determine ScreenSize. A Normal ScreenSize will be returned.");
+ return NORMAL;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 0984e13

Please sign in to comment.