Android Open Source - chat.android Sync Resources Activity






From Project

Back to project page chat.android.

License

The source code is released under:

GNU General Public License

If you think the Android project chat.android 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 org.chat.android;
//from   www . j  ava  2  s  .  co m
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

import javax.net.ssl.HttpsURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.chat.android.Auth.AccountGeneral;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SyncResourcesActivity extends Activity {
  private TextView textView1;
  private EditText editText1;
  private String clientToken;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.syncresources);
        textView1 = (TextView) findViewById(R.id.textView1);
        editText1 = (EditText) findViewById(R.id.editText1);
    }
    
    /** Called when the user selects the Send button */
    public void sendReq(View view) {
      textView1.setText("");
      
      ConnectivityManager connMgr = (ConnectivityManager) 
              getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
          
      if (networkInfo != null && networkInfo.isConnected()) {
        // ensure we only download big files if we are connected via WiFi
        // http://developer.android.com/reference/android/net/NetworkInfo.html#getType%28%29
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            // fetch data
          if (editText1.getText().toString().isEmpty()) {
            new RequestTask().execute(getApplicationContext().getResources().getString(R.string.base_url));
          } else {
            new RequestTask().execute(editText1.getText().toString());            
          }
        } else {
          textView1.setText("Not connected via WiFi. Aborting download");
        }
      } else {
          // display error
        textView1.setText("No network connection active");
      } 
    }
    
    private class RequestTask extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... uri) {
          boolean downloadSuccess = false;
//        String baseUrl = getApplicationContext().getResources().getString(R.string.base_url);
        String baseUrl = uri[0];
//          String paramToken = "?client_access_token="+clientToken;
          
          String urlAssets = baseUrl + "/assets/";
          urlAssets = urlAssets.replaceAll("(?<!(http:|https:))//", "/");
//          urlAssets = urlAssets.concat(paramToken);
          
          try {
            // retrieve list of all videos
            HttpURLConnection urlConnection = (HttpURLConnection) new URL(urlAssets).openConnection();                      
            // unsure connection times out after 10 seconds
                urlConnection.setConnectTimeout(10000);
                ArrayList<String> videoFileNames = new ArrayList<String>();
                
                // checking response to see if it worked ok
                if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
//                  Log.i("SyncAdapter","Status code: "+urlConnection.getResponseCode()+" and status line: "+urlConnection.getResponseMessage());
                  try {
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                    String videos = convertStreamToString(in);
                    JSONArray jsonArray = new JSONArray(videos);
                    Log.i("SyncAdapter", "Downloading the following assets: "+jsonArray.toString());
                    // put JSONArray into a ArrayList
                      for(int i = 0, count = jsonArray.length(); i< count; i++)
                      {
                          try {
                            videoFileNames.add((String)jsonArray.get(i));
                          }
                          catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
//                        videoFileNames = convertJSONtoStringArray(jsonArray);
                    } catch (JSONException e) {            
              e.printStackTrace();
            } 
                  finally {
                      urlConnection.disconnect();
                    }
                } else {
                  urlConnection.disconnect();
                }
                
                downloadSuccess = true;
                Iterator<String> iter = videoFileNames.iterator();
                // download all video files in the list of video file names
                while (iter.hasNext() && downloadSuccess) {
                  String videoFile = iter.next();
                  // http://stackoverflow.com/questions/6045377/how-to-insert-20-in-place-of-space-in-android
                  String urlFileName = videoFile.replaceAll(" ", "%20");
                  
                  String urlAsset = baseUrl + "/asset/"+urlFileName;
                  urlAsset = urlAsset.replaceAll("(?<!(http:|https:))//", "/");              
//                  urlAsset = urlAsset.concat(paramToken);
                  Log.i("SyncAdapter", "Downloading from "+urlAsset);
                  
                  HttpURLConnection urlConn = (HttpURLConnection) new URL(urlAsset).openConnection();
                  // make connection time out after 10 seconds
                  urlConn.setConnectTimeout(10000);           
                  
                  // checking response to see if it worked ok
                  if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK){
                    Log.i("SyncAdapter","Status code: "+urlConn.getResponseCode()+" and status line: "+urlConn.getResponseMessage());
                    try {
                      InputStream in = new BufferedInputStream(urlConn.getInputStream());                
                      downloadSuccess = storeInputStreamInFile(in, videoFile,urlConn.getContentLength());
                      }
                      finally {                    
                        urlConn.disconnect();
                      }
                  } else {
                    urlConn.disconnect();
                  }
                }
                
                
                if (downloadSuccess) {
                return "Download success";
              } else {
                return "Download failure";
              }
          } catch (IOException e) {
            e.printStackTrace();
            return "Download failure";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            //Do anything with response..
            textView1.setText(result);

        }
        
        // http://stackoverflow.com/questions/10752919/how-can-i-convert-inputstream-data-to-string-in-android-soap-webservices
      private String convertStreamToString(InputStream is) {
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          StringBuilder sb = new StringBuilder();

          String line = null;
          try {
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  is.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return sb.toString();
      }
      
      // this will be useful so that you can show a tipical 0-100%
        // progress bar
      private boolean storeInputStreamInFile(InputStream input, String fileName, int lengthOfFile) {
        int count;
            try {
              File videoFile = new File(Environment.getExternalStorageDirectory().toString() +"/chat/" +fileName);
              
                // Output stream
                OutputStream output = new FileOutputStream(videoFile);

                byte data[] = new byte[8192];
                long downloadedSize = 0;
                int percentDownloaded;
                int previous = -1;
                
                Log.i("SyncResources", "Downloading to file: "+videoFile.getAbsolutePath());

                while ((count = input.read(data)) > 0) {
                  downloadedSize += count;
                    //this is where you would do something to report the prgress, like this maybe
                  percentDownloaded = (int) ((downloadedSize * 100) / lengthOfFile);
                  if (percentDownloaded % 10 == 0 && percentDownloaded > previous) {
                    Log.d("Progress: ","downloaded "+percentDownloaded+" percent of "+ lengthOfFile + " Bytes");
                    previous = percentDownloaded;
                  }

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();
                
                Log.i("SyncResources", "Finished downloading to file: "+videoFile.getAbsolutePath());
                
                return true;
            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
                return false;
            }
      }
    }
    
    private ArrayList<String> convertJSONtoStringArray (JSONArray jsonArray) {
      ArrayList<String> stringArray = new ArrayList<String>();
      for(int i = 0, count = jsonArray.length(); i< count; i++)
      {
          try {
              stringArray.add((String)jsonArray.get(i));
          }
          catch (JSONException e) {
              e.printStackTrace();
          }
      }
      return stringArray;
    }
}




Java Source Code List

org.chat.android.BaseActivity.java
org.chat.android.CHADelivery.java
org.chat.android.CHAOverviewActivity.java
org.chat.android.CHASelectChildActivity.java
org.chat.android.CHASelectChildAdapter.java
org.chat.android.ChatUtil.java
org.chat.android.ClientsAdapter.java
org.chat.android.DatabaseHelper.java
org.chat.android.GPSTracker.java
org.chat.android.HealthDeliveryActivity.java
org.chat.android.HealthDetailsActivity.java
org.chat.android.HealthOverviewActivity.java
org.chat.android.HealthOverviewRecordActivity.java
org.chat.android.HomeActivity.java
org.chat.android.ImmunizationsReceivedActivity.java
org.chat.android.ImmunizationsSummaryActivity.java
org.chat.android.LoginActivity.java
org.chat.android.Mail.java
org.chat.android.ModelHelper.java
org.chat.android.MyApplication.java
org.chat.android.ResourcesActivity.java
org.chat.android.RestoreVisitActivity.java
org.chat.android.ServiceDeliveryActivity.java
org.chat.android.ServiceDeliveryAdapter.java
org.chat.android.ServiceDetailsActivity.java
org.chat.android.ServiceOtherActivity.java
org.chat.android.ServiceOverviewActivity.java
org.chat.android.ServicesAdapter.java
org.chat.android.SetupDB.java
org.chat.android.SetupVisitActivity.java
org.chat.android.SyncResourcesActivity.java
org.chat.android.Auth.AccountGeneral.java
org.chat.android.Auth.AuthenticatorService.java
org.chat.android.Auth.Authenticator.java
org.chat.android.Auth.ChatAuthServerAuthenticate.java
org.chat.android.Auth.MainActivity.java
org.chat.android.Auth.ServerAuthenticate.java
org.chat.android.Sync.StubProvider.java
org.chat.android.Sync.SyncAdapter.java
org.chat.android.Sync.SyncService.java
org.chat.android.Sync.provider.ChatContentProvider.java
org.chat.android.models.Attendance.java
org.chat.android.models.CHAAccessed.java
org.chat.android.models.Client.java
org.chat.android.models.HealthPage.java
org.chat.android.models.HealthSelectRecorded.java
org.chat.android.models.HealthSelect.java
org.chat.android.models.HealthTheme.java
org.chat.android.models.HealthTopicAccessed.java
org.chat.android.models.HealthTopic.java
org.chat.android.models.Household.java
org.chat.android.models.PageAssessment1.java
org.chat.android.models.PageSelect1.java
org.chat.android.models.PageText1.java
org.chat.android.models.PageVideo1.java
org.chat.android.models.ResourceAccessed.java
org.chat.android.models.Resource.java
org.chat.android.models.Role.java
org.chat.android.models.ServiceAccessed.java
org.chat.android.models.Service.java
org.chat.android.models.TopicVideo.java
org.chat.android.models.Util.java
org.chat.android.models.VaccineRecorded.java
org.chat.android.models.Vaccine.java
org.chat.android.models.VideoAccessed.java
org.chat.android.models.Video.java
org.chat.android.models.Visit.java
org.chat.android.models.Worker.java
org.chat.android.pages.Assessment1Fragment.java
org.chat.android.pages.BaseFragment.java
org.chat.android.pages.ReferralFragment.java
org.chat.android.pages.Select1Fragment.java
org.chat.android.pages.Text1Fragment.java
org.chat.android.pages.Video1Fragment.java