Guest User

Untitled

a guest
Aug 19th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package com.example.simpledownloader.sharable;
  2.  
  3. import java.util.Timer;
  4. import java.util.Vector;
  5.  
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.net.ConnectivityManager;
  10. import android.os.Handler;
  11. import android.util.Log;
  12.  
  13. import com.example.simpledownloader.adapter.TaskAdapter;
  14. import com.example.simpledownloader.scheduler.Scheduler;
  15. import com.example.simpledownloader.sql.TaskDAO;
  16. import com.example.simpledownloader.task.Task;
  17.  
  18. public class Sharable {
  19.     public static Context cx = null;
  20.     public static Vector<Task> downloads = null;
  21.     public static int networkOption = 1;
  22.     public static TaskDAO taskDAO = null;
  23.     public static SQLiteDatabase db = null;
  24.     public static ConnectivityManager connMgr = null;
  25.     public static TaskAdapter adapter = null;
  26.     public static Timer schedulerTimer = null;
  27.     public static Scheduler schedulerTask = null;
  28.     private static boolean shouldLook = true;
  29.     public static Handler handler = null;
  30. //--------------------------------------------------------------------------------
  31.     public Sharable(Context con){
  32.         cx = con; // Pass application context and not activity context
  33.         taskDAO = new TaskDAO(con); // Make connection to database
  34.         db = taskDAO.getWritableDatabase(); // get a reference to database
  35.         db.enableWriteAheadLogging(); // allow multiple threads writing to database
  36.         downloads = Sharable.getTasks(); // get the tasks
  37.         adapter = new TaskAdapter(con);
  38.         schedulerTimer = new Timer("Scheduler", true); // Must be started later by another activity
  39.         schedulerTask = new Scheduler();
  40.         connMgr = (ConnectivityManager) cx.getSystemService(Context.CONNECTIVITY_SERVICE); // get network and wifi service
  41.     }
  42. //--------------------------------------------------------------------------------
  43.     private static Vector<Task> getTasks() {
  44.         String query = "SELECT name, url, contentlength, byteswritten FROM tasks";
  45.         Cursor resultSet = db.rawQuery(query, null); // select all rows from the table
  46.         Vector<Task> tasks = new Vector<Task>(); // make a vector
  47.         resultSet.moveToFirst(); // move to the first row
  48.         if(resultSet.getCount() == 0){ // if the result has zero rows
  49.             Log.v("SHARABLE", "EMPTY");
  50.             return tasks;  // return empty task
  51.         }else{
  52.             while(resultSet.moveToNext()){ // else we must populate the vector
  53.                 String name = resultSet.getString(0); // get the name of the file
  54.                 String url = resultSet.getString(1); // get the URL for the file
  55.                 long contentLength = resultSet.getLong(2); // get its length, from HTTP connection
  56.                 long bytesWritten = resultSet.getLong(3); // get the bytes written
  57.                 Task temp = new Task(name, url, contentLength, bytesWritten); // create a Task object
  58.                 if(temp.getProgress() != 100){
  59.                     tasks.add(temp); // add it to vector
  60.                     temp.setReadiness(true);
  61.                     Log.v("SHARABLE", temp.toString());
  62.                 }
  63.             }
  64.             return tasks;
  65.         }
  66.     }
  67. //--------------------------------------------------------------------------------
  68.     public static synchronized boolean getShouldLook(){
  69.         return shouldLook;
  70.     }
  71. //--------------------------------------------------------------------------------
  72.     public static void setShouldLook(boolean look){
  73.         shouldLook = look;
  74.     }
  75. //--------------------------------------------------------------------------------
  76. }
Advertisement
Add Comment
Please, Sign In to add comment