Android Open Source - dissertation-project G C M Intent Service






From Project

Back to project page dissertation-project.

License

The source code is released under:

MIT License

If you think the Android project dissertation-project listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fyp.resilience;
//from   ww  w .j  a  v a2  s  .c o  m
import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;

import com.fyp.resilience.database.model.DataPiece;
import com.fyp.resilience.database.model.DataWhole;
import com.fyp.resilience.event.ServerRegistrationChanged;
import com.fyp.resilience.register.Register.Devices;
import com.fyp.resilience.register.model.DeviceInfo;
import com.fyp.resilience.service.PieceUploadService;
import com.fyp.resilience.util.Utils;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;

import de.greenrobot.event.EventBus;

/**
 * An intent service which runs within its own thread. Is responsible for
 * Registration to GCM and the Widerst server. As well as receiving Messages
 * from the Widerst server via GCM.
 */
public class GCMIntentService extends GCMBaseIntentService {

    private Devices mDevicesEndpoint;
    private static final String SENDER_ID = "136104787243";
    private static final String TAG = GCMIntentService.class.getSimpleName();

    /**
     * Register the device for GCM.
     * 
     * @param context the activity's context.
     */
    public static void register(final Context context) {
        GCMRegistrar.checkDevice(context);
        GCMRegistrar.checkManifest(context);
        GCMRegistrar.register(context, SENDER_ID);
    }

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mDevicesEndpoint = ResilienceApplication.getApplication(getApplicationContext()).getRegistrationService()
                .devices();
    }

    /**
     * Called on registration error. This is called in the context of a Service
     * - no dialog or UI.
     * 
     * @param context the Context
     * @param errorId an error message
     */
    @Override
    public void onError(final Context context, final String errorId) {
        Log.i(TAG, "GCM has errored  " + errorId);
    }

    /**
     * Called when a cloud message has been received.
     */
    @Override
    public void onMessage(final Context context, final Intent intent) {

        final ResilienceController controller = ResilienceApplication.getApplication(this).getResilienceController();
        final String dataWholeKey = intent.getExtras().getString("whole_completed");
        final String dataPieceKey = intent.getExtras().getString("piece_request");

        if (null != dataWholeKey) {

            final DataWhole dataWhole = controller.getDataWholeById(dataWholeKey);

            Log.i(TAG, "DataWhole " + dataWholeKey + " is available on the server");

            if (null != dataWhole && (dataWhole.getUriString() == null || dataWhole.getUriString().equals(""))) {
                dataWhole.setAvailability(true);
                dataWhole.setState(DataWhole.STATE_COMPLETED);
                controller.removeDataPieces(dataWhole);
                controller.addDataWhole(dataWhole);
            }

        } else if (null != dataPieceKey) {
            
            Log.d(TAG, dataPieceKey);

            final String requestWholeKey = intent.getExtras().getString("whole_request");

            if (null != requestWholeKey) {
                final DataWhole dataWhole = controller.getDataWholeById(requestWholeKey);

                if (null != dataWhole) {

                    /* Check if the user has already downloaded the Whole */
                    if (!dataWhole.isAvailable()) {

                        List<DataPiece> pieces = dataWhole.getPieces();
                        if (null != pieces) {
                            for (DataPiece dataPiece : pieces) {
                                if (dataPiece.getKey().equals(dataPieceKey)) {
                                    dataPiece.setRetry(true);
                                    dataWhole.setState(DataWhole.STATE_NONE);
                                    controller.addDataWhole(dataWhole);
                                    if (Utils.canUploadToServer(this)) {
                                        startService(new Intent(this, PieceUploadService.class));
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Called when a registration token has been received.
     * 
     * @param context the Context
     */
    @Override
    public void onRegistered(final Context context, final String registration) {
        Log.i(TAG, "Device has been registered to GCM with " + registration);
        try {

            if (!GCMRegistrar.isRegisteredOnServer(context)) {
                /*
                 * If the device has NEVER been registered then inform the
                 * server of the new device
                 */
                final DeviceInfo deviceInfo = mDevicesEndpoint.insert(
                        new DeviceInfo().setDeviceRegistrationId(registration))
                        .execute();
                if (null != deviceInfo.getServerRegistrationId()) {
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .edit()
                            .putString(PreferenceConstants.SERVER_ID_KEY,
                                    deviceInfo.getServerRegistrationId())
                            .commit();

                    GCMRegistrar.setRegisteredOnServer(context, true);
                }

            } else {
                /*
                 * If the device has been registered but this is a NEW
                 * registration then update the server of a change
                 */
                final String serverId = Utils.getDeviceInfo(context).getServerRegistrationId();
                final DeviceInfo deviceInfo = new DeviceInfo()
                        .setServerRegistrationId(serverId)
                        .setDeviceRegistrationId(registration);

                mDevicesEndpoint.update(deviceInfo).execute();
            }

            EventBus.getDefault().post(new ServerRegistrationChanged());

        } catch (IOException e) {
            e.printStackTrace();
        }

        if (Utils.canUploadToServer(this)) {
            startService(new Intent(this, PieceUploadService.class));
        }

    }

    /**
     * Called when the device has been unregistered.
     * 
     * @param context the Context
     */
    @Override
    protected void onUnregistered(final Context context, final String registrationId) {
    }
}




Java Source Code List

com.fyp.resilience.Constants.java
com.fyp.resilience.Flags.java
com.fyp.resilience.GCMIntentService.java
com.fyp.resilience.PreferenceConstants.java
com.fyp.resilience.ResilienceApplication.java
com.fyp.resilience.ResilienceController.java
com.fyp.resilience.activity.LicenceActivity.java
com.fyp.resilience.activity.ResilienceActivity.java
com.fyp.resilience.activity.SettingsActivity.java
com.fyp.resilience.adapter.ClientListAdapter.java
com.fyp.resilience.adapter.ConnectionListAdapter.java
com.fyp.resilience.adapter.FileListAdapter.java
com.fyp.resilience.connection.Connectable.java
com.fyp.resilience.connection.ServerDownloadConnectable.java
com.fyp.resilience.connection.ServerUploadConnectable.java
com.fyp.resilience.connection.UploadConnectable.java
com.fyp.resilience.connection.WifiDownloadConnectable.java
com.fyp.resilience.connection.WifiUploadConnectable.java
com.fyp.resilience.database.ResilienceDbHelper.java
com.fyp.resilience.database.ResilienceDbManager.java
com.fyp.resilience.database.model.DataPiece.java
com.fyp.resilience.database.model.DataWhole.java
com.fyp.resilience.event.ClientListChanged.java
com.fyp.resilience.event.ClientModified.java
com.fyp.resilience.event.ConnectionProgressChange.java
com.fyp.resilience.event.ConnectionStateChange.java
com.fyp.resilience.event.ConnectionsModified.java
com.fyp.resilience.event.PieceStateChange.java
com.fyp.resilience.event.ServerRegistrationChanged.java
com.fyp.resilience.event.ServerUploadFinished.java
com.fyp.resilience.event.WholeModified.java
com.fyp.resilience.event.WifiDownloadFinished.java
com.fyp.resilience.event.WifiUploadFinished.java
com.fyp.resilience.fragment.ClientsFragment.java
com.fyp.resilience.fragment.ConnectionsFragment.java
com.fyp.resilience.fragment.FilesFragment.java
com.fyp.resilience.interfaces.Messagable.java
com.fyp.resilience.interfaces.Partialable.java
com.fyp.resilience.proto.ProtoBuffSpecification.java
com.fyp.resilience.receiver.AbstractConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.BootReceiver.java
com.fyp.resilience.receiver.ConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver.java
com.fyp.resilience.register.RegisterRequestInitializer.java
com.fyp.resilience.register.RegisterRequest.java
com.fyp.resilience.register.RegisterScopes.java
com.fyp.resilience.register.Register.java
com.fyp.resilience.register.model.DeviceInfo.java
com.fyp.resilience.service.PieceUploadService.java
com.fyp.resilience.stream.PiecedRandomAccessFile.java
com.fyp.resilience.swarm.helper.NsdHelper.java
com.fyp.resilience.swarm.helper.SwarmHelperInterface.java
com.fyp.resilience.swarm.helper.WifiDirectSdHelper.java
com.fyp.resilience.swarm.model.SwarmClient.java
com.fyp.resilience.thread.ResilienceRunnable.java
com.fyp.resilience.thread.ResilienceThreadFactory.java
com.fyp.resilience.util.ConnectionUtils.java
com.fyp.resilience.util.Utils.java
com.fyp.resilience.view.ClientView.java
com.fyp.resilience.view.ConnectionView.java
com.fyp.resilience.view.FileView.java
com.fyp.resilience.view.PieceProgressIndicator.java
com.fyp.resilience.widerst.WiderstRequestInitializer.java
com.fyp.resilience.widerst.WiderstRequest.java
com.fyp.resilience.widerst.WiderstScopes.java
com.fyp.resilience.widerst.Widerst.java
com.fyp.resilience.widerst.model.DataPiecePartial.java
com.fyp.resilience.widerst.model.DataWholePartial.java
com.fyp.resilience.widerst.model.PostResponse.java
com.fyp.widerst.Constants.java
com.fyp.widerst.WiderstObjectifyService.java
com.fyp.widerst.backend.FileJoinerBackend.java
com.fyp.widerst.cron.CronJobServlet.java
com.fyp.widerst.endpoint.DataPieceEndpoint.java
com.fyp.widerst.endpoint.DeviceInfoEndpoint.java
com.fyp.widerst.entity.DataPiece.java
com.fyp.widerst.entity.DataWhole.java
com.fyp.widerst.entity.DeviceInfo.java
com.fyp.widerst.handler.BlobstoreUploadHandler.java
com.fyp.widerst.partial.DataPiecePartial.java
com.fyp.widerst.partial.DataWholePartial.java
com.fyp.widerst.response.PostResponse.java
com.fyp.widerst.servlet.WholeFileServer.java
com.fyp.widerst.util.DbHelper.java
com.fyp.widerst.util.GcmHelper.java