/// <summary> /// 开启或关闭Adb服务 /// </summary> /// <param name="start">表示开启服务或结束服务</param> /// <param name="error"></param> /// <returns></returns> public static bool ToggleServer(bool start, Action <ErrorResult> error) { try { var process = new Process(); var info = new ProcessStartInfo { FileName = AdbPath, Arguments = start? "start-server":"kill-server", StandardErrorEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8, RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, UseShellExecute = false }; process.StartInfo = info; process.Start(); process.WaitForExit(30 * 1000); return(process.ExitCode == 0); } catch (Exception e) { ErrorResult.InvokeError(error, InitError.AdbNotExists, e.Message); return(false); } }
/// <summary> /// 获取Adb内部版本,可能出现的错误,无法连接 /// </summary> /// <param name="error">错误回调</param> /// <returns></returns> public static bool VerifyVersion(Action <ErrorResult> error) { // 连接到ADB服务 var socket = SocketHelper.Connect(); if (socket == null) { // 无法链接到ADB服务 ErrorResult.InvokeError(error, ConnectError.AdbServerUnable, "Socket 无法链接到ADB服务端(5037)端口!"); return(false); } // 执行查询内部版本命令 const string cmd = "host:version"; socket.Send(Encoding.ASCII.GetBytes($"{cmd.Length:X4}{cmd}")); // 读取结果,包含状态和版本 var res = SocketHelper.GetResult(socket); var flag = res.Okay && string.Equals(res.Data, AdbVersion); try { socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch (Exception) { // ignored } return(flag); }
/// <summary> /// 解析提取备份文件 /// </summary> /// <param name="dest"></param> /// <param name="progress"></param> /// <returns></returns> public bool Extract(string dest, Action <long, long> progress) { if (context == null) { return(false); } // 检查是否为备份文件 if (!Check()) { return(false); } // 获取文件总长度 var total = context.Query <FileData>().Sum(data => data.Length); var current = 0L; // 查询非文件夹文件 var infos = context.Query <FileInfo>().Where(info => info.Index >= 0); // 遍历解析文件 foreach (var info in infos.OrderBy(info => info.Index).ToList()) { // 确保路径可用 var path = dest + info.Path.Replace("/", "\\"); var regex = new Regex($@"[{string.Concat(Path.GetInvalidFileNameChars())}]"); path = regex.Replace(path, "_"); // 提取文件 try { // 创建文件夹 var dir = Path.GetDirectoryName(path); if (dir != null) { Directory.CreateDirectory(dir); } // 创建文件 var stream = File.OpenWrite(path); var blobs = context.Query <FileData>().Where(data => data.FileIndex == info.Index); // 循环写入文件 foreach (var blob in blobs.OrderBy(data => data.DataIndex).ToList()) { current += blob.Length; stream.Write(blob.Data, 0, Convert.ToInt32(blob.Length)); // 进度回调 progress?.Invoke(current, total); } stream.Close(); } catch (Exception e) { ErrorResult.InvokeError(error, CommonError.InvalidDirectory, path); Logger.Error(e); } } return(true); }
/// <summary> /// 创建备份解析器 /// </summary> /// <param name="path"></param> /// <param name="des"></param> /// <param name="error"></param> /// <returns></returns> public static BackupParser Create(string path, string des, Action <ErrorResult> error) { try { // 创建保存目录 Directory.CreateDirectory(des); } catch (Exception e) { Logger.Error(e); ErrorResult.InvokeError(error, CommonError.InvalidDirectory, des); return(null); } return(new BackupParser(path, des, error)); }
/// <summary> /// 从手机端下载文件 /// </summary> /// <param name="path">远程文件路径</param> /// <param name="dest">本地保存地址</param> /// <param name="progress">进度回调</param> /// <param name="error"></param> public bool GetFile(string path, string dest, Action <long, long> progress, Action <ErrorResult> error) { // 检查连接情况 if (!TryConnect(error)) { return(false); } var flag = false; try { // 处理目录和文件名 if (path.StartsWith("/")) { path = path.Substring(1); } var full = Path.Combine(dest, path); dest = Path.GetDirectoryName(full); if (string.IsNullOrEmpty(dest)) { return(false); } if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); } var cmd = Command.Create("File", path); var are = new AutoResetEvent(false); var file = new FileHandler(Serial) { DataHandler = progress, Exit = success => { flag = success; are.Set(); } }; file.BeginGetFile(cmd.ToString(), full, error); are.WaitOne(); } catch (Exception e) { Logger.Error(e); ErrorResult.InvokeError(error, CommonError.InvalidDirectory, dest); } return(flag); }
/// <summary> /// 解析华为备份 /// </summary> /// <param name="progress"></param> /// <returns></returns> public bool ParseHuaWeiBackup(Action <int, string> progress) { var flag = false; if (!Directory.Exists(path)) { ErrorResult.InvokeError(error, CommonError.InvalidDirectory, path); return(false); } // 解析文件夹 foreach (var file in Directory.GetFiles(path)) { var hb = HbFile.Create(file, error); //flag = hb.Extract(des, pro => //{ // progress?.Invoke(pro, file); //}); } return(flag); }