Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.simpledownloader.sharable;
- import java.util.Timer;
- import java.util.Vector;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.net.ConnectivityManager;
- import android.os.Handler;
- import android.util.Log;
- import com.example.simpledownloader.adapter.TaskAdapter;
- import com.example.simpledownloader.scheduler.Scheduler;
- import com.example.simpledownloader.sql.TaskDAO;
- import com.example.simpledownloader.task.Task;
- public class Sharable {
- public static Context cx = null;
- public static Vector<Task> downloads = null;
- public static int networkOption = 1;
- public static TaskDAO taskDAO = null;
- public static SQLiteDatabase db = null;
- public static ConnectivityManager connMgr = null;
- public static TaskAdapter adapter = null;
- public static Timer schedulerTimer = null;
- public static Scheduler schedulerTask = null;
- private static boolean shouldLook = true;
- public static Handler handler = null;
- //--------------------------------------------------------------------------------
- public Sharable(Context con){
- cx = con; // Pass application context and not activity context
- taskDAO = new TaskDAO(con); // Make connection to database
- db = taskDAO.getWritableDatabase(); // get a reference to database
- db.enableWriteAheadLogging(); // allow multiple threads writing to database
- downloads = Sharable.getTasks(); // get the tasks
- adapter = new TaskAdapter(con);
- schedulerTimer = new Timer("Scheduler", true); // Must be started later by another activity
- schedulerTask = new Scheduler();
- connMgr = (ConnectivityManager) cx.getSystemService(Context.CONNECTIVITY_SERVICE); // get network and wifi service
- }
- //--------------------------------------------------------------------------------
- private static Vector<Task> getTasks() {
- String query = "SELECT name, url, contentlength, byteswritten FROM tasks";
- Cursor resultSet = db.rawQuery(query, null); // select all rows from the table
- Vector<Task> tasks = new Vector<Task>(); // make a vector
- resultSet.moveToFirst(); // move to the first row
- if(resultSet.getCount() == 0){ // if the result has zero rows
- Log.v("SHARABLE", "EMPTY");
- return tasks; // return empty task
- }else{
- while(resultSet.moveToNext()){ // else we must populate the vector
- String name = resultSet.getString(0); // get the name of the file
- String url = resultSet.getString(1); // get the URL for the file
- long contentLength = resultSet.getLong(2); // get its length, from HTTP connection
- long bytesWritten = resultSet.getLong(3); // get the bytes written
- Task temp = new Task(name, url, contentLength, bytesWritten); // create a Task object
- if(temp.getProgress() != 100){
- tasks.add(temp); // add it to vector
- temp.setReadiness(true);
- Log.v("SHARABLE", temp.toString());
- }
- }
- return tasks;
- }
- }
- //--------------------------------------------------------------------------------
- public static synchronized boolean getShouldLook(){
- return shouldLook;
- }
- //--------------------------------------------------------------------------------
- public static void setShouldLook(boolean look){
- shouldLook = look;
- }
- //--------------------------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment