Remove stream activities from sample sync adapter
Also fixed existing bug that prevented the "Add Connection" from working for the Ssample Sync Adapter. Bug: 11629361 Change-Id: I17940cc97ebef3e05dc53e534d5765395ba353a3
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* 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 com.example.android.samplesync.activities;
|
||||
|
||||
import com.example.android.samplesync.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Activity to handle view a stream-item. In a real app, this would show a rich view of the
|
||||
* item.
|
||||
*/
|
||||
public class ViewStreamItemActivity extends Activity {
|
||||
private static final String TAG = "ViewStreamItemActivity";
|
||||
|
||||
private TextView mUriTextView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.view_stream_item_activity);
|
||||
|
||||
mUriTextView = (TextView) findViewById(R.id.view_stream_item_uri);
|
||||
mUriTextView.setText(getIntent().getDataString());
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* 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 com.example.android.samplesync.activities;
|
||||
|
||||
import com.example.android.samplesync.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Activity to view a stream-item-photo. In a real app, this would show a fullscreen view of the
|
||||
* photo, potentially with ways to interact with it
|
||||
*/
|
||||
public class ViewStreamItemPhotoActivity extends Activity {
|
||||
private static final String TAG = "ViewStreamItemPhotoActivity";
|
||||
|
||||
private TextView mUriTextView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.view_stream_item_photo_activity);
|
||||
|
||||
mUriTextView = (TextView) findViewById(R.id.view_stream_item_photo_uri);
|
||||
mUriTextView.setText(getIntent().getDataString());
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,7 @@ import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Email;
|
||||
@@ -41,14 +38,8 @@ import android.provider.ContactsContract.Groups;
|
||||
import android.provider.ContactsContract.RawContacts;
|
||||
import android.provider.ContactsContract.Settings;
|
||||
import android.provider.ContactsContract.StatusUpdates;
|
||||
import android.provider.ContactsContract.StreamItemPhotos;
|
||||
import android.provider.ContactsContract.StreamItems;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -243,43 +234,6 @@ public class ContactManager {
|
||||
batchOperation.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrate how to add stream items and stream item photos to a raw
|
||||
* contact. This just adds items for all of the contacts for this sync
|
||||
* adapter with some locally created text and an image. You should check
|
||||
* for stream items on the server that you are syncing with and use the
|
||||
* text and photo data from there instead.
|
||||
*
|
||||
* @param context The context of Authenticator Activity
|
||||
* @param rawContacts The list of users we want to update
|
||||
*/
|
||||
public static void addStreamItems(Context context, List<RawContact> rawContacts,
|
||||
String accountName, String accountType) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
final BatchOperation batchOperation = new BatchOperation(context, resolver);
|
||||
String text = "This is a test stream item!";
|
||||
String message = "via SampleSyncAdapter";
|
||||
for (RawContact rawContact : rawContacts) {
|
||||
addContactStreamItem(context, lookupRawContact(resolver,
|
||||
rawContact.getServerContactId()), accountName, accountType,
|
||||
text, message, batchOperation );
|
||||
}
|
||||
List<Uri> streamItemUris = batchOperation.execute();
|
||||
|
||||
// Stream item photos are added after the stream items that they are
|
||||
// associated with, using the stream item's ID as a reference.
|
||||
|
||||
for (Uri uri : streamItemUris){
|
||||
// All you need is the ID of the stream item, which is the last index
|
||||
// path segment returned by getPathSegments().
|
||||
long streamItemId = Long.parseLong(uri.getPathSegments().get(
|
||||
uri.getPathSegments().size()-1));
|
||||
addStreamItemPhoto(context, resolver, streamItemId, accountName,
|
||||
accountType, batchOperation);
|
||||
}
|
||||
batchOperation.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* After we've finished up a sync operation, we want to clean up the sync-state
|
||||
* so that we're ready for the next time. This involves clearing out the 'dirty'
|
||||
@@ -595,59 +549,6 @@ public class ContactManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a stream item to a raw contact. The stream item is usually obtained
|
||||
* from the server you are syncing with, but we create it here locally as an
|
||||
* example.
|
||||
*
|
||||
* @param context the Authenticator Activity context
|
||||
* @param rawContactId the raw contact ID that the stream item is associated with
|
||||
* @param accountName the account name of the sync adapter
|
||||
* @param accountType the account type of the sync adapter
|
||||
* @param text the text of the stream item
|
||||
* @param comments the comments for the stream item, such as where the stream item came from
|
||||
* @param batchOperation allow us to batch together multiple operations
|
||||
*/
|
||||
private static void addContactStreamItem(Context context, long rawContactId,
|
||||
String accountName, String accountType, String text, String comments,
|
||||
BatchOperation batchOperation) {
|
||||
|
||||
final ContentValues values = new ContentValues();
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
if (rawContactId > 0){
|
||||
values.put(StreamItems.RAW_CONTACT_ID, rawContactId);
|
||||
values.put(StreamItems.TEXT, text);
|
||||
values.put(StreamItems.TIMESTAMP, System.currentTimeMillis());
|
||||
values.put(StreamItems.COMMENTS, comments);
|
||||
values.put(StreamItems.ACCOUNT_NAME, accountName);
|
||||
values.put(StreamItems.ACCOUNT_TYPE, accountType);
|
||||
|
||||
batchOperation.add(ContactOperations.newInsertCpo(
|
||||
StreamItems.CONTENT_URI, false, true).withValues(values).build());
|
||||
}
|
||||
}
|
||||
|
||||
private static void addStreamItemPhoto(Context context, ContentResolver
|
||||
resolver, long streamItemId, String accountName, String accountType,
|
||||
BatchOperation batchOperation){
|
||||
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
|
||||
R.raw.img1);
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
|
||||
byte[] photoData = stream.toByteArray();
|
||||
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(StreamItemPhotos.STREAM_ITEM_ID, streamItemId);
|
||||
values.put(StreamItemPhotos.SORT_INDEX, 1);
|
||||
values.put(StreamItemPhotos.PHOTO, photoData);
|
||||
values.put(StreamItems.ACCOUNT_NAME, accountName);
|
||||
values.put(StreamItems.ACCOUNT_TYPE, accountType);
|
||||
|
||||
batchOperation.add(ContactOperations.newInsertCpo(
|
||||
StreamItems.CONTENT_PHOTO_URI, false, true).withValues(values).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the local system 'dirty' flag for a contact.
|
||||
*
|
||||
|
||||
@@ -119,18 +119,6 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
||||
|
||||
ContactManager.updateStatusMessages(mContext, updatedContacts);
|
||||
|
||||
// This is a demo of how you can add stream items for contacts on
|
||||
// the client. This probably won't apply to
|
||||
// 2-way contact sync providers - it's more likely that one-way
|
||||
// sync providers (IM clients, social networking apps, etc) would
|
||||
// use this feature. This is only supported in ICS MR1 or above.
|
||||
|
||||
if (Build.VERSION.SDK_INT >=
|
||||
Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
|
||||
ContactManager.addStreamItems(mContext, updatedContacts,
|
||||
account.name, account.type);
|
||||
}
|
||||
|
||||
// Save off the new sync marker. On our next sync, we only want to receive
|
||||
// contacts that have changed since this sync...
|
||||
setServerSyncMarker(account, newSyncState);
|
||||
|
||||
Reference in New Issue
Block a user