FYI. In case anyone would benefit, here is the code I used to get the
Picasa example app to point to Google Docs. I've been using this in an
app for a year now, and no problems whatsoever. It will upload only -
download is another example (haven't had a need for it, so didnt write
it yet. But should be straightforward enough to do on your own).
Please spend some time digesting how Google setup the Picasa sample
before reading this code or emailing me. You'll have to wrap you head
around the concepts before moving on.
DISCLAIMER: Offered as a courtesy to teach - this should be just
enough code to get you going in the right direction.
- Best regards, Nick
--------------------------------- part 1
------------------------------------------------------
public final class GoogleDocumentsListAtom {
/**
* XML namespace dictionary.
*
*/
public static final XmlNamespaceDictionary NAMESPACE_DICTIONARY =
new
XmlNamespaceDictionary();
static {
Map<String, String> map =
NAMESPACE_DICTIONARY.namespaceAliasToUriMap;
map.put("", "http://www.w3.org/2005/Atom");
map.put("app", "http://www.w3.org/2007/app");
map.put("atom", "http://www.w3.org/2005/Atom");
map.put("batch", "http://schemas.google.com/gdata/batch");
map.put("docs", "http://schemas.google.com/docs/2007");
map.put("gAcl", "http://schemas.google.com/acl/2007");
map.put("gd", "http://schemas.google.com/g/2005");
map.put("openSearch", "http://a9.com/-/spec/opensearch/1.1/");
map.put("xml", "http://www.w3.org/XML/1998/namespace");
}
private GoogleDocumentsListAtom() {
}
}
------------------------------------------------- part 2
----------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
myCtx = this;
boolean doSend = false;
if (intent.getData() != null &&
intent.getData().toString().contains("gdocs-upload")) {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
mProgDialog = ProgressDialog.show(this, "", "Uploading...",
true);
mProgDialog.show();
doSend = true;
new UploadFilesTask(this).execute(this);
}
else {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webintent);
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new MyWebViewClient(this));
mProgDialog = ProgressDialog.show(this, "", "Loading Page...",
true);
mProgDialog.show();
}
transport.applicationName = "google-
docsmyapplicationnamegoeshere";
HttpTransport.setLowLevelHttpTransport(ApacheHttpTransport.INSTANCE);
SharedPreferences settings = getSharedPreferences(PREF, 0);
setLogging(settings.getBoolean("logging", false));
if (Intent.ACTION_SEND.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
if (extras != null &&
extras.containsKey(getResources().getString( R.string.bundle_filename )))
{
String fname = extras.getString("filename");
String key =
getResources().getString(R.string.bundle_filename);
mFileName =
extras.getString(getResources().getString( R.string.bundle_filename ));
// mFileName = null; force an error condition
key = getResources().getString(R.string.bundle_filelen);
mFileLength =
extras.getInt(getResources().getString( R.string.bundle_filelen ));
if (mFileName == null || mFileLength < 0) {
// not initialized in caller
String msg = "mFileLength not initialized in
call to Upload.";
Exception e = new Exception(msg);
handleException(e);
}
}
try {
sendData = new SendData(intent, getContentResolver());
} catch (Exception e) {
returnWithError("Upload error " + e.getMessage() );
}
}
else
if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
sendData = null;
}
if (doSend == false) {
gotAccount(false);
}
}
private static OAuthHmacSigner createOAuthSigner() {
OAuthHmacSigner result = new OAuthHmacSigner();
if (credentials != null) {
result.tokenSharedSecret = credentials.tokenSecret;
}
result.clientSharedSecret = "anonymous";
return result;
}
private static OAuthParameters createOAuthParameters() {
OAuthParameters authorizer = new OAuthParameters();
authorizer.consumerKey = "anonymous";
authorizer.signer = createOAuthSigner();
authorizer.token = credentials.token;
return authorizer;
}
private void gotAccount(boolean tokenExpired) {
switch (AUTH_TYPE) {
case OAUTH:
try {
boolean isViewAction =
Intent.ACTION_VIEW.equals(getIntent().getAction());
if (tokenExpired && !isTemporary && credentials != null) {
GoogleOAuthGetAccessToken
.revokeAccessToken(createOAuthParameters());
credentials = null;
}
if (tokenExpired || !isViewAction
&& (isTemporary || credentials == null)) {
GoogleOAuthGetTemporaryToken temporaryToken =
new GoogleOAuthGetTemporaryToken();
temporaryToken.signer = createOAuthSigner();
temporaryToken.consumerKey = "anonymous";
temporaryToken.scope = GoogleDocumentsList.ROOT_URL;
temporaryToken.displayName =
"<<<your specific information goes here>>>";
temporaryToken.callback = "gdocs-upload:///";
isTemporary = true;
credentials = temporaryToken.execute();
GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl =
new GoogleOAuthAuthorizeTemporaryTokenUrl();
authorizeUrl.temporaryToken = credentials.token;
Intent webIntent = new Intent(Intent.ACTION_VIEW);
webIntent.setData(Uri.parse(authorizeUrl.build()));
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int
progress)
{
activity.setProgress(progress * 1000);
}
});
Uri myUri = webIntent.getData();
webview.setWebViewClient(new MyWebViewClient(this));
webview.loadUrl(myUri.toString());
}
else
{
if (isViewAction)
{
Uri uri = this.getIntent().getData();
OAuthCallbackUrl callbackUrl =
new OAuthCallbackUrl(uri.toString());
GoogleOAuthGetAccessToken accessToken =
new GoogleOAuthGetAccessToken();
accessToken.temporaryToken = callbackUrl.token;
accessToken.verifier = callbackUrl.verifier;
accessToken.signer = createOAuthSigner();
accessToken.consumerKey = "anonymous";
isTemporary = false;
credentials = accessToken.execute();
createOAuthParameters().signRequestsUsingAuthorizationHeader(
transport);
}
authenticated();
}
}
catch (Exception e) {
this.mProgDialog.cancel();
returnWithError("Upload failed " + e.getMessage());
}
return;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_AUTHENTICATE:
if (resultCode == RESULT_OK) {
gotAccount(false);
} else {
returnWithMessage("Complete");
}
break;
}
}
static class SendData {
String fileName;
Uri uri;
String contentType;
long contentLength;
SendData(Intent intent, ContentResolver contentResolver) throws
IOException {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = this.uri = (Uri)
extras.getParcelable(Intent.EXTRA_STREAM);
String scheme = uri.getScheme();
if (scheme.equals("content")) {
Cursor cursor = contentResolver.query(uri, null, null, null,
null);
cursor.moveToFirst();
this.fileName =
cursor.getString(cursor
.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
this.contentType = intent.getType();
this.contentLength =
cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media.SIZE));
}
}
else {
String fName =
myCtx.getResources().getString(R.string.filestoredir) + mFileName;
File directory = new
File(Environment.getExternalStorageDirectory().getPath()+fName);
Uri fileUri = Uri.fromFile(directory);
this.uri = fileUri;
this.fileName = mFileName;
this.contentType = "text/plain";
this.contentLength = mFileLength;
}
}
}
static SendData sendData;
private void authenticated() {
if (sendData != null) {
try {
if (sendData.fileName != null) {
try {
HttpRequest request = transport.buildPostRequest();
GoogleUrl simpleUrl = new
GoogleUrl(GoogleDocumentsList.ROOT_URL);
simpleUrl.path += "default/private/full";
request.url = simpleUrl;
GoogleHeaders.setSlug(request.headers, sendData.fileName);
InputStreamContent content = new InputStreamContent();
content.inputStream =
getContentResolver().openInputStream(sendData.uri);
content.type = sendData.contentType;
content.length = sendData.contentLength;
request.content = content;
request.execute().ignore();
credentials = null;
this.mProgDialog.cancel();
returnWithSuccess("Successful upload");
} catch (IOException e) {
returnWithError("Upload failed " + e.getMessage());
}
}
}
finally
{
sendData = null;
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en