Android Open Source - android-davsync Network Receiver






From Project

Back to project page android-davsync.

License

The source code is released under:

Copyright ? 2013, Michael Stapelberg and contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project android-davsync 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 net.zekjur.davsync;
/*from   w w w  .  java 2s  .c  o m*/
import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;

public class NetworkReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d("davsync", "network connectivity changed");

    if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
      return;

    ConnectivityManager cs = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = cs.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
      Log.d("davsync", "_NOT_ connected anymore, not doing anything.");
      return;
    }

    SharedPreferences preferences = context.getSharedPreferences("net.zekjur.davsync_preferences",
        Context.MODE_PRIVATE);

    boolean syncOnWifiOnly = preferences.getBoolean("auto_sync_on_wifi_only", true);

    if (syncOnWifiOnly && !(ConnectivityManager.TYPE_WIFI == info.getType())) {
      Log.d("davsync", "Not on WIFI, not doing anything.");
      return;
    }

    Log.d("davsync", "Checking whether pictures need to be synced");

    // XXX: It doesnt really feel right to do this blockingly in a
    // BroadcastReceiver, but I was unable to find whether this is the right
    // way or whether there is a better one.

    DavSyncOpenHelper helper = new DavSyncOpenHelper(context);
    ArrayList<String> uris = helper.getQueuedUris();
    for (String uri : uris) {
      Intent ulIntent = new Intent(context, UploadService.class);
      // evtl: mapintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      ulIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uri));
      context.startService(ulIntent);
    }
  }

}




Java Source Code List

net.zekjur.davsync.CountingInputStreamEntity.java
net.zekjur.davsync.DavSyncOpenHelper.java
net.zekjur.davsync.NetworkReceiver.java
net.zekjur.davsync.NewMediaReceiver.java
net.zekjur.davsync.SettingsActivity.java
net.zekjur.davsync.ShareActivity.java
net.zekjur.davsync.UploadService.java