Android Open Source - PreCTS Cts Media Download Dialog






From Project

Back to project page PreCTS.

License

The source code is released under:

Apache License

If you think the Android project PreCTS 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 com.androidhuman.ctsprepare.dialog;
//  w w w .  j  a v a  2 s .c  o m
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.androidhuman.ctsprepare.util.Download;

public class CtsMediaDownloadDialog extends Dialog implements Observer{

  protected Object result;
  protected Shell shell;
  private ProgressBar progressBar;
  private Button btnCancel;
  private Label lblStatus;
  
  Download download;

  /**
   * Create the dialog.
   * @param parent
   * @param style
   */
  public CtsMediaDownloadDialog(Shell parent, int style) {
    super(parent, style);
    setText("CTS media");
  }

  /**
   * Open the dialog.
   * @return the result
   */
  public Object open() {
    createContents();
    shell.open();
    shell.layout();
    Display display = getParent().getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    return result;
  }

  /**
   * Create contents of the dialog.
   */
  private void createContents() {
    shell = new Shell(getParent(), SWT.MIN | SWT.TITLE);
    shell.setSize(586, 108);
    shell.setText(getText());
    
    lblStatus = new Label(shell, SWT.NONE);
    lblStatus.setBounds(10, 10, 276, 17);
    lblStatus.setText("Downloading...");
    
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setBounds(10, 33, 566, 17);
    
    btnCancel = new Button(shell, SWT.NONE);
    btnCancel.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent arg0) {
        if(download.getStatus()==Download.DOWNLOADING){
          download.cancel();
        }
        shell.close();
      }
    });
    btnCancel.setBounds(487, 66, 89, 27);
    btnCancel.setText("Cancel");

    try {
      download = new Download(new URL("https://dl.google.com/dl/android/cts/android-cts-media-1.0.zip"));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    download.addObserver(this);
    
    new Thread(new Runnable(){
      public void run(){
        while(download.getStatus()==Download.DOWNLOADING){
          Display.getDefault().asyncExec(new Runnable(){
            public void run(){
              progressBar.setSelection((int)download.getProgress());
            }
          });
          System.out.println("Download prog : "+download.getProgress());
          try{ Thread.sleep(1000); }catch(Exception e){};
        }
      }
    }).start();
    
    download.download();
  }

  @Override
  public void update(Observable arg0, Object arg1) {
    Display.getDefault().asyncExec(new Runnable(){
      public void run(){
        switch(download.getStatus()){
        case Download.ERROR:
          lblStatus.setText("Download error");
          btnCancel.setText("Close");
          break;
        case Download.COMPLETE:
          lblStatus.setText("Download completed.");
          break;
        }  
      }
    });
    
  }
}




Java Source Code List

com.androidhuman.ctsprepare.automator.GoogleAccount.java
com.androidhuman.ctsprepare.automator.PreCTSAutomate.java
com.androidhuman.ctsprepare.automator.WifiAp.java
com.androidhuman.ctsprepare.data.BasicDeviceInfo.java
com.androidhuman.ctsprepare.data.Event.java
com.androidhuman.ctsprepare.data.GoogleAccount.java
com.androidhuman.ctsprepare.data.Model.java
com.androidhuman.ctsprepare.data.ProxySettings.java
com.androidhuman.ctsprepare.data.Task.java
com.androidhuman.ctsprepare.data.Waiver.java
com.androidhuman.ctsprepare.data.WifiAp.java
com.androidhuman.ctsprepare.dialog.CtsMediaDownloadDialog.java
com.androidhuman.ctsprepare.dialog.EditGoogleAccountDialog.java
com.androidhuman.ctsprepare.dialog.EditMacroDialog.java
com.androidhuman.ctsprepare.dialog.EditProxyDialog.java
com.androidhuman.ctsprepare.dialog.EditSdkPathDialog.java
com.androidhuman.ctsprepare.dialog.EditWifiApDialog.java
com.androidhuman.ctsprepare.dialog.TargetSelectionDialog.java
com.androidhuman.ctsprepare.shell.ConfigureMacroShell.java
com.androidhuman.ctsprepare.shell.Main.java
com.androidhuman.ctsprepare.util.AdbCommand.java
com.androidhuman.ctsprepare.util.AdbWrapper.java
com.androidhuman.ctsprepare.util.AsyncTask.java
com.androidhuman.ctsprepare.util.Download.java
com.androidhuman.ctsprepare.util.Utils.java
com.androidhuman.ctsprepare.util.Worker.java
com.androidhuman.prectsandroidsettingsapp.EnableSyncActivity.java
com.androidhuman.prectsandroidsettingsapp.FinishHandler.java
com.androidhuman.prectsandroidsettingsapp.MainActivity.java