/*
* Copyright (c) 2013 Google Inc.
*
* 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.google.cloud.backend.android;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
/**
* An {@link Activity} class that wraps CloudBackend features including CRUD of
* {@link CloudEntity}, Google account authentication and Google Cloud
* Messaging. Developer may create a subclass to use these features. If you want
* to inherit other {@link Activity} subclasses, you may rewrite the superclass
* name of the class definition below.
*
*/
public class CloudBackendActivity extends Activity {
private CloudBackendMessaging cloudBackend;
// is this app subscribed to the Cloud Message?
private static boolean isSubscribedToBroadcastMessage = false;
/**
* Returns {@link CloudBackendMessaging} instance for this activity.
*
* @return {@link CloudBackendMessaging}
*/
protected CloudBackendMessaging getCloudBackend() {
return cloudBackend;
}
/**
* Subclasses may override this to execute initialization of the activity.
* If it uses any CloudBackend features, it should be executed inside
* {@link #onPostCreate()} that will be called after CloudBackend
* initializations such as user authentication.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init backend
cloudBackend = new CloudBackendMessaging(this);
// post create initialization
_onPostCreate();
}
private void _onPostCreate() {
// init subscription to broadcast message
if (!isSubscribedToBroadcastMessage) {
isSubscribedToBroadcastMessage = true;
CloudCallbackHandler<List<CloudEntity>> handler = new CloudCallbackHandler<List<CloudEntity>>() {
@Override
public void onComplete(List<CloudEntity> results) {
onBroadcastMessageReceived(results);
}
};
cloudBackend.subscribeToCloudMessage(CloudBackendMessaging.TOPIC_ID_BROADCAST, handler);
}
// call onPostCreate
onPostCreate();
}
/**
* Will be called after initialization process including the account
* picking. Subclasses may override to execute any initialization of the
* activity.
*/
protected void onPostCreate() {
}
/**
* Handles broadcast messages from the backend. Subclasses may override this
* to handle the message accordingly.
*
* @param message
* {@link CloudEntity} that contains the message values.
*/
public void onBroadcastMessageReceived(List<CloudEntity> message) {
}
}