Yang Zhang avatar Yang Zhang committed 5728a7e

almost done

Comments (0)

Files changed (10)

android/res/layout/download_chooser.xml

             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal" >
-
+
             <Button
                 android:id="@+id/select_all"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center_horizontal"
                 android:layout_weight="1"
-                android:text="选" />
+                android:text="选" />
 
             <Button
                 android:id="@+id/downloadSureButton"

android/src/me/yangblog/manmanpa/ChapterListActivity.java

 package me.yangblog.manmanpa;
 
+import java.util.LinkedList;
+
 import me.yangblog.manmanpa.model.Book;
 import me.yangblog.manmanpa.model.Chapter;
 import me.yangblog.manmanpa.network.JSONParser;
 		final Dialog dialog = new Dialog(ChapterListActivity.this);
 		View viewToLoad = LayoutInflater.from(ChapterListActivity.this).inflate(R.layout.download_chooser, null);
 		GridView downloadGd =  (GridView) viewToLoad.findViewById(R.id.downloadGradview);
-		Button button = (Button) viewToLoad.findViewById(R.id.downloadSureButton);
+		Button downloadButton = (Button) viewToLoad.findViewById(R.id.downloadSureButton);
+		Button selectAllButton = (Button) viewToLoad.findViewById(R.id.select_all);
 		final DownloadChapterAdapter dcha = new DownloadChapterAdapter(this, book);
 		downloadGd.setAdapter(dcha);
+		setSelectAllButton(selectAllButton,dcha );
 		
-		button.setOnClickListener(new OnClickListener() {
+		
+		
+		downloadButton.setOnClickListener(new OnClickListener() {
 			
 			@Override
 			public void onClick(View v) {
+				LinkedList<Chapter> tempList = dcha.getSelectChapter();
+				if(!tempList.isEmpty()){
+					GlobalVariable.downloader.setDs(new DownloadSource(ChapterListActivity.this));
+					GlobalVariable.downloader.addChapterList(tempList,book.getName() );
+					if (!GlobalVariable.downloader.isRunning()) {
+						new Thread(GlobalVariable.downloader).start();
+					}
+					Toast.makeText(ChapterListActivity.this, "���ؿ�ʼ...", Toast.LENGTH_SHORT).show();
+				}
 				dialog.dismiss();
-				Toast.makeText(ChapterListActivity.this, "���ؿ�ʼ...", Toast.LENGTH_SHORT).show();
+				
 			}
 		});
 		
 		dialog.setContentView(viewToLoad);
 		dialog.show();
 	}
+	
+	private void setSelectAllButton(final Button selectAllButton, final DownloadChapterAdapter dcha){
+		selectAllButton.setOnClickListener(new OnClickListener() {
+			@Override
+			public void onClick(View v) {
+				dcha.selectALl();
+				setUnSelect(selectAllButton,dcha);
+				
+			}
+		});
+	}
+	
+	private void setUnSelect(final Button selectAllButton, final DownloadChapterAdapter dcha){
+		selectAllButton.setOnClickListener(new OnClickListener() {
+			@Override
+			public void onClick(View v) {
+				dcha.unSelectAll();
+				setSelectAllButton(selectAllButton,dcha);
+			}
+		});
+	}
+	
+	
 
 	@Override
 	public void parseJSON(JSONObject jObj) throws JSONException {

android/src/me/yangblog/manmanpa/DownloadAdapter.java

 				public void run() {
 					while (!downloaCh.isDownloaded()) {
 						try {
-							Thread.sleep(1000);
+							Thread.sleep(400);
 						} catch (InterruptedException e) {
 							e.printStackTrace();
 						}
 								pb.setProgress(downloaCh.getDownloadedNumber());
 							}
 						});
+						
 
 					}
 					handler.post(new Runnable() {
 							stateText.setText("�����");
 							stopOrResumeButton.setText("���");
 							stopOrResumeButton.setEnabled(false);
+							DownloadAdapter.this.notifyDataSetChanged();
 						}
 					});
 

android/src/me/yangblog/manmanpa/DownloadChapterAdapter.java

 package me.yangblog.manmanpa;
 
-import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.List;
+import java.util.LinkedList;
 import java.util.Set;
 
 import me.yangblog.manmanpa.model.Book;
 import me.yangblog.manmanpa.model.Chapter;
-import me.yangblog.manmanpa.reader.ComicReaderActivity;
 import android.content.Context;
-import android.content.Intent;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
-import android.widget.BaseAdapter;
-import android.widget.Button;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.ToggleButton;
 
 public class DownloadChapterAdapter extends ChapterAdapter {
 	private Set<Chapter> downloadChapters = new HashSet<Chapter>();
-	
+	private Context context;
+	private boolean selectAll = false;
+
 	public DownloadChapterAdapter(ChapterListActivity context, Book book) {
-		super(context,book);
-
+		super(context, book);
+		this.context = context;
 	}
 
-	
-
 	@Override
 	public View getView(final int position, View convertView, ViewGroup parent) {
+
 		ToggleButton button = new ToggleButton(context);
 		button.setMinLines(2);
 		final Chapter currentCh = this.chapterList.get(position);
 		button.setText(currentCh.getName());
 		button.setTextOff(currentCh.getName());
 		button.setTextOn(currentCh.getName());
+		if (selectAll) {
+			button.setChecked(true);
+		}
+
 		button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
-			
+
 			@Override
-			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-				if(isChecked){
+			public void onCheckedChanged(CompoundButton buttonView,
+					boolean isChecked) {
+				if (isChecked) {
 					downloadChapters.add(currentCh);
-				}else if(downloadChapters.contains(currentCh)){
+				} else if (downloadChapters.contains(currentCh)) {
 					downloadChapters.remove(currentCh);
 				}
 			}
 		});
-		
+
 		button.setOnClickListener(new OnClickListener() {
-			
+
 			@Override
 			public void onClick(View v) {
-				
+
 			}
 		});
 
 		return button;
 	}
 
+	public void selectALl() {
+		selectAll = true;
+		notifyDataSetChanged();
+	}
+
+	public void unSelectAll() {
+		selectAll = false;
+		notifyDataSetChanged();
+	}
+
+	public LinkedList<Chapter> getSelectChapter() {
+		LinkedList<Chapter> tempList = new LinkedList<Chapter>();
+		tempList.addAll(downloadChapters);
+		return tempList;
+	}
+
 }

android/src/me/yangblog/manmanpa/download/ChapterDownloader.java

 package me.yangblog.manmanpa.download;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import me.yangblog.manmanpa.network.JSONParser;
 import me.yangblog.manmanpa.sql.DownloadSource;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.util.Log;
 
 public class ChapterDownloader {
 	private Chapter chapter;
 	public ChapterDownloader(Chapter ch, String bookname) {
 		this.chapter = ch;
 		this.bookname = bookname;
+
+	}
+	
+	public void init_pageList(){
 		if (Helper.isChapterJsonFileCached(chapter.getId() + ".ch")) {
 			initPageList();
 		} else {
-			jsonPaser.execute(GlobalVariable.PAGE_URL + chapter.getId());
+			downLoad_init();
 		}
-		ch.setPageList(pageList);
+		chapter.setPageList(pageList);
+	}
 
+	
+	private void downLoad_init(){
+		JSONObject jObj  = getJsonObject(GlobalVariable.PAGE_URL + chapter.getId());
+		try {
+			_parseJSON(jObj);
+		} catch (JSONException e) {
+			e.printStackTrace();
+		}
 	}
 
 	private void initPageList() {
 	}
 
 	public boolean isInitial() {
-		return pageList != null && !pageList.isEmpty();
+		if(pageList ==null || pageList.isEmpty()){
+			return false;
+		}
+		return true;
 	}
 
 	public void stop() {
 
 		return inputStream;
 	}
+	
+	public JSONObject getJsonObject(String url) {
+		InputStream is = null;
+		JSONObject jObj = null;
+		String json = "";
+		// Making HTTP request
+		try {
+			// defaultHttpClient
+			DefaultHttpClient httpClient = new DefaultHttpClient();
+			HttpGet httpGet = new HttpGet(url);
+
+			HttpResponse httpResponse = httpClient.execute(httpGet);
+			HttpEntity httpEntity = httpResponse.getEntity();
+			is = httpEntity.getContent();
+
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+
+		try {
+			BufferedReader reader = new BufferedReader(new InputStreamReader(
+					is, "iso-8859-1"), 8);
+			StringBuilder sb = new StringBuilder();
+			String line = null;
+			while ((line = reader.readLine()) != null) {
+				sb.append(line + "\n");
+			}
+			is.close();
+			json = sb.toString();
+		} catch (Exception e) {
+			//Toast.makeText(fa, "�������Ӵ���", Toast.LENGTH_LONG).show();
+			Log.e("Buffer Error", "Error converting result " + e.toString());
+			return null;
+		}
+
+		// try parse the string to a JSON object
+		try {
+			jObj = new JSONObject(json);
+		} catch (JSONException e) {
+			Log.e("JSON Parser", "Error parsing data " + e.toString());
+		}
+		return jObj;
+	}
+
+	
+	
 
 	public void setDs(DownloadSource ds) {
 		this.ds = ds;

android/src/me/yangblog/manmanpa/download/Downloader.java

 import java.util.Map;
 import java.util.Queue;
 
+import me.yangblog.manmanpa.GlobalVariable;
 import me.yangblog.manmanpa.NotifyAble;
 import me.yangblog.manmanpa.model.Book;
 import me.yangblog.manmanpa.model.Chapter;
 	}
 
 	public void addTask(ChapterDownloader chd) {
-//		chd.saveInfor();
+		chd.saveInfor();
 		int bookid = chd.getChapter().getBookid();
 		if (taskQueueMap.containsKey(bookid)) {
 			taskQueueMap.get(bookid).add(chd);
+			return;
 		}
 		LinkedList<ChapterDownloader> tempList = new LinkedList<ChapterDownloader>();
 		tempList.add(chd);
 		}
 	}
 
+	
+	public void addChapterList(List<Chapter> chList, String bookname) {
+		for (Chapter ch : chList) {
+			ChapterDownloader temp = new ChapterDownloader(ch, bookname);
+			temp.setDs(ds);
+			this.addTask(temp);
+		}
+	}
+	
 	public Chapter getCurrentDownloadingChapter() {
 		if (currentChd == null) {
 			return null;
 				LinkedList<ChapterDownloader> taskQueue = taskQueueMap.get(key);
 				while (!taskQueue.isEmpty()) {
 					currentChd = taskQueue.removeFirst();
+					if(!currentChd.isInitial()){
+						currentChd.init_pageList();
+					}
+					
 					currentChd.startDownload(notifyAble);
 				}
 				currentChd = null;

android/src/me/yangblog/manmanpa/model/Chapter.java

 	private int downloadedNumber = 0;
 	
 	
-	private List<Page> pageList;
+	private List<Page> pageList = new LinkedList<Page>();
 	
 	
 	

android/src/me/yangblog/manmanpa/network/JSONParser.java

 	static InputStream is = null;
 	JSONObject jObj = null;
 	static String json = "";
-	private Activity fa;
+	private Activity fa =null;
 	private final ProgressDialog dialog;
 	JSONNeedParser jnp;
 	private boolean  isRun;

android/src/me/yangblog/manmanpa/reader/ComicReaderActivity.java

 		hs.updateCurrentPage(chapter, currentPageNumber+1);
 		
 	}
+	
+	
 }
 
 
-1. ���������ļ�, ��ʾ����---------> downloader + notifiable-----> ������...
 2. ��ǩ + ����
     * ������
     * json����, ������Ǻ���
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.