diff --git a/Android/samples/sdksample/app/src/main/java/com/microsoft/rome/onesdksample_android/BaseService.java b/Android/samples/sdksample/app/src/main/java/com/microsoft/rome/onesdksample_android/BaseService.java index e6a65f5..e4ac2cc 100644 --- a/Android/samples/sdksample/app/src/main/java/com/microsoft/rome/onesdksample_android/BaseService.java +++ b/Android/samples/sdksample/app/src/main/java/com/microsoft/rome/onesdksample_android/BaseService.java @@ -11,6 +11,7 @@ import com.microsoft.connecteddevices.base.EventListener; import com.microsoft.connecteddevices.commanding.AppServiceConnection; import com.microsoft.connecteddevices.commanding.AppServiceRequestReceivedEventArgs; import com.microsoft.connecteddevices.discovery.AppServiceDescription; +import com.microsoft.connecteddevices.hosting.AppServiceConnectionOpenedEventArgs; import com.microsoft.connecteddevices.hosting.AppServiceProvider; /** @@ -40,7 +41,8 @@ public abstract class BaseService implements AppServiceProvider, EventListener listener) { + UserDataFeed feed = UserDataFeed.getForAccount(account, PlatformBroker.getPlatform(), Secrets.APP_HOST_NAME); + feed.addSyncStatusChangedListener(listener); + feed.addSyncScopes(scopes); + feed.startSync(); + return feed; + } + + public void initializeUserActivityFeed() { + mStatusText = getStringValue(R.string.status_activities_initialize); Log.d(TAG, mStatusText); - UserActivityChannel channel = null; try { + UserAccount[] accounts = AccountProviderBroker.getSignInHelper().getUserAccounts(); + if (accounts.length <= 0) { + mStatusText = getStringValue(R.string.status_activities_signin_required); + Log.e(TAG, mStatusText); + return; + } + // Step #1 - // create a UserActivityChannel for the signed in account - channel = new UserActivityChannel(mUserDataFeed); + // get the UserDataFeed for the signed in account + UserDataFeedSyncScope[] scopes = { UserActivityChannel.getSyncScope() }; + mUserDataFeed = getUserDataFeed(accounts[0], scopes, new EventListener() { + @Override + public void onEvent(UserDataFeed userDataFeed, Void aVoid) { + if (userDataFeed.getSyncStatus() == UserDataFeedSyncStatus.SYNCHRONIZED) { + mStatusText = getStringValue(R.string.status_activities_initialize_complete); + Log.e(TAG, mStatusText); + } else { + mStatusText = getStringValue(R.string.status_activities_initialize_failed); + Log.e(TAG, mStatusText); + } + } + }); + + mStatusText = getStringValue(R.string.status_activities_get_channel); + Log.d(TAG, mStatusText); + + // Step #2 + // create a UserActivityChannel on the UserDataFeed + mActivityChannel = new UserActivityChannel(mUserDataFeed); mStatusText = getStringValue(R.string.status_activities_get_channel_success); Log.d(TAG, mStatusText); @@ -101,34 +132,13 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi mStatusText = getStringValue(R.string.status_activities_get_channel_failed); Log.e(TAG, mStatusText); } - return channel; - } - - private UserDataFeed getUserDataFeed(SyncScope[] scopes, EventListener listener) { - UserAccount[] accounts = AccountProviderBroker.getSignInHelper().getUserAccounts(); - if (accounts.length <= 0) { - mStatusText = getStringValue(R.string.status_activities_signin_required); - Log.e(TAG, mStatusText); - return null; - } - - UserDataFeed feed = UserDataFeed.getForAccount(accounts[0], PlatformBroker.getPlatform(), Secrets.APP_HOST_NAME); - feed.addSyncStatusChangedListener(listener); - feed.addSyncScopes(scopes); - feed.startSync(); - return feed; - } - - private String createActivityId() { - return UUID.randomUUID().toString(); } @Nullable private UserActivity createUserActivity(UserActivityChannel channel, String activityId) { UserActivity activity = null; - AsyncOperation activityOperation = channel.getOrCreateUserActivityAsync(activityId); - try { + AsyncOperation activityOperation = channel.getOrCreateUserActivityAsync(activityId); activity = activityOperation.get(); mStatusText = getStringValue(R.string.status_activities_create_activity_success); Log.d(TAG, mStatusText); @@ -140,30 +150,6 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi return activity; } - /** - * Initializes the UserActivityFeed. - */ - public void initializeUserActivityFeed() { - mStatusText = getStringValue(R.string.status_activities_initialize); - Log.d(TAG, mStatusText); - - SyncScope[] scopes = { UserActivityChannel.getSyncScope(), UserNotificationChannel.getSyncScope() }; - mUserDataFeed = getUserDataFeed(scopes, new EventListener() { - @Override - public void onEvent(UserDataFeed userDataFeed, Void aVoid) { - if (userDataFeed.getSyncStatus() == UserDataSyncStatus.SYNCHRONIZED) { - mStatusText = getStringValue(R.string.status_activities_initialize_complete); - Log.d(TAG, mStatusText); - } else { - mStatusText = getStringValue(R.string.status_activities_initialize_failed); - Log.e(TAG, mStatusText); - } - } - }); - - mActivityChannel = getUserActivityChannel(); - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_useractivity, container, false); @@ -184,8 +170,8 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi mReadButton = rootView.findViewById(R.id.activityReadButton); mReadButton.setOnClickListener(this); - mHistoryItems = new ArrayList<>(); mListView = rootView.findViewById(R.id.activityListView); + mHistoryItems = new ArrayList<>(); mListAdapter = new UserActivityListAdapter(getContext(), mHistoryItems); mListView.setAdapter(mListAdapter); @@ -196,18 +182,19 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi @Override public void onClick(View v) { - // New activity button clicked - /* - Clicking the New button will generate default properties for the activity, create the activity, - and publish it. - */ + // Create New activity button clicked + // Clicking it will generate default values for the activity, create the activity and publish it. if (mNewButton.equals(v)) { + setStatus(R.string.status_activities_create_activity); + mActivity = null; - final String newId = createActivityId(); + mActivitySession = null; + + final String newActivityId = UUID.randomUUID().toString(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { - mActivityId.setText(newId); + mActivityId.setText(newActivityId); mDisplayText.setText(R.string.default_activity_display_text); mActivationUri.setText(R.string.default_activity_uri); mActivityIconUri.setText(R.string.default_activity_icon_uri); @@ -215,8 +202,6 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi } }); - setStatus(R.string.status_activities_create_activity); - // ActivityChannel has not been initialized if (mActivityChannel == null) { setStatus(R.string.status_activities_channel_null); @@ -263,7 +248,7 @@ public class UserActivityFragment extends BaseFragment implements View.OnClickLi setStatus(R.string.status_activities_session_start); } else { - mActivitySession.close(); + mActivitySession.stop(); mActivitySession = null; getActivity().runOnUiThread(new Runnable() { @Override diff --git a/Android/samples/sdksample/app/src/main/res/values/strings.xml b/Android/samples/sdksample/app/src/main/res/values/strings.xml index 06681c6..3fbd291 100644 --- a/Android/samples/sdksample/app/src/main/res/values/strings.xml +++ b/Android/samples/sdksample/app/src/main/res/values/strings.xml @@ -82,7 +82,7 @@ Failed to save UserActivity! Read UserActivity successfully Failed to read UserActivity! - ActivityChannel is null (is platform initialization complete?) + UserActivityChannel is not available yet (is platform initialization complete?) Reading UserActivities… Must create UserActivity before starting session! UserActivitySession started