Android Open Source - PreCTS Utils






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.util;
// w  w w. j  a v  a2s.c  o  m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.androidhuman.ctsprepare.data.ProxySettings;

public class Utils {
  
  public interface ProgressListener{
    public void onPrepare(long totalLength);
    public void onProgress(long current);
    public void onFinished();
  }
  
  public static void getConfirmMessageBox(Shell shell, String title, String message){
    MessageBox msg = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
    msg.setText(title);
    msg.setMessage(message);
    msg.open();
  }
  
  public static void showErrorMessage(Shell shell, String message){
    MessageBox msg = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
    msg.setText("Error");
    msg.setMessage(message);
    msg.open();
  }
  
  public static void showInfoMessageBox(Shell shell, String message){
    MessageBox msg = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
    msg.setText("Information");
    msg.setMessage(message);
    msg.open();
  }
  
  public static boolean showYesNoMessageBox(Shell shell, String message){
    MessageBox msg = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.YES | SWT.NO);
    msg.setText("Question");
    msg.setMessage(message);
    int result = msg.open();
    return result==SWT.YES;
  }
  
  public static void copyFile(File inFile, File outFile) throws IOException{
    FileChannel source = null;
    FileChannel dest = null;
    
    FileInputStream fis = new FileInputStream(inFile);
    FileOutputStream fos = new FileOutputStream(outFile);
    source = fis.getChannel();
    dest = fos.getChannel();
       
    dest.transferFrom(source, 0, source.size());
     
      
    if(source!=null){
      source.close();
    }
    if(fis!=null){
      fis.close();
    }
    
    if(dest!=null){
      dest.close();
    }
    if(fos!=null){
      fos.close();
    }
      
  }
  
  public static boolean isWindows(){
    return System.getProperty("os.name").toLowerCase().indexOf("win") >=0 ? true : false;
  }
  
  public static String getSeparator(){
    return isWindows() ? "\\" : "/";
  }
  
  public static void copyFile(File inFile, File outFile, ProgressListener listener) {
    if(listener==null){
      throw new IllegalStateException("Listener cannot be null");
    }
    
    try {
      FileInputStream fis = new FileInputStream(inFile);
      FileOutputStream fos = new FileOutputStream(outFile);
         
      listener.onPrepare(inFile.length());
      int data = 0;
      long bytes = 0;
      
      while((data=fis.read())!=-1) {
        fos.write(data);
        listener.onProgress(++bytes);
      }
      listener.onFinished();
      
      fis.close();
      fos.close();
       
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  public static void setAdbPath(String path) throws IllegalArgumentException{
    File file = new File(path);
    
    if(!file.isDirectory()){
      throw new IllegalArgumentException("Not a valid directory.");
    }
    
    // Check adb executable existence
    String separator = getSeparator();
    StringBuilder b = new StringBuilder().append(path).append(separator).append("platform-tools").append(separator).append("adb");
    if(isWindows()){
      b.append(".exe");
    }
    File adb = new File(b.toString());
    if(!adb.exists() || !adb.isFile()){
      throw new IllegalArgumentException("adb binary not found. Try download the SDK again.");
    }
    
    // adb is placed in right place
    FileWriter writer = null;
    try{
      writer = new FileWriter("environment.info");
      writer.write(path);
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      if(writer!=null){
        try{ writer.close(); }catch(IOException e){}
      }
    }
  }
  
  public static String getAdbPath(){
    String path = null;
    File file = new File("environment.info");
    if(!file.exists()){
      return null;
    }
    
    BufferedReader reader = null;
    
    try{
      reader = new BufferedReader(new FileReader(file));
      path = reader.readLine();
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      if(reader!=null){
        try{ reader.close(); }catch(IOException e){}
      }
    }
    return path;
  }
  
  public static void setProxySettings(ProxySettings settings){
    if(settings==null){
      throw new IllegalArgumentException("ProxySettings cannot be null");
    }
    File file = new File("proxy.info");
    FileWriter writer = null;
    try{
      if(!file.exists()){
        file.createNewFile();
      }
      writer = new FileWriter(file);
      writer.write(settings.toJson());
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      if(writer!=null){
        try{ writer.close(); }catch(IOException e){}
      }
    }
  }
  
  public static ProxySettings getProxySettings(){
    File file = new File("proxy.info");
    ProxySettings settings = null;
    if(!file.exists()){
      // Return new ProxySettings
      settings = new ProxySettings();
      return settings;
    }
    BufferedReader reader = null;
    try{
      reader = new BufferedReader(new FileReader(file));
      settings = ProxySettings.fromJson(reader.readLine());
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      if(reader!=null){
        try{ reader.close(); }catch(IOException e){}
      }
    }
    return settings;
  }
  
  public static boolean isCtsMediaExists(){
    File mediaDir = new File("cts_media");
    File bbbShort = new File(mediaDir, "bbb_short");
    File bbbFull = new File(mediaDir, "bbb_full");
    
    if(!mediaDir.exists()){
      return false;
    }else{
      if(!bbbShort.exists()){
        return false;
      }
      if(!bbbFull.exists()){
        return false;
      }
    }
    return true;
  }
}




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