Wednesday, November 9, 2016

Visual Basic အားအသံုးျပဳျပီး Android Tool တစ္ခုေရးသားျခင္း

Microsoft Visual Basic ေလ့လာမယ့္သူေတြအတြက္ XDA က Post တစ္ခုကုိ
ဘာသာျပန္ေရးသားထားပါတယ္။

Project တစ္ခုစတင္တည္ေဆာက္ျခင္း

Visual Studio ကုိဖြင့္ပါ။ ေအာက္ကလုိ Welcome Page ေပၚလာပါမယ္။



'New Project...' ကုိေရြးပါ။
ေအာက္ကလုိေပၚလာပါမယ္။


 'Windows Forms Application' ကိုေရြးျပီးႏွစ္သက္ရာနာမည္ေပးလုိက္ပါ။
ဒီမွာေတာ့  Android One-Click Tutorial လုိ႕ပဲေပးထားပါတယ္။

အားလံုးျပီးတာနဲ႕ OK ကုိႏွိပ္ျပီး Project အသစ္တည္ေဆာက္လုိက္ပါ။


One-Click adb tool ေလးေရးၾကည့္ပါမယ္။

Project စတင္ျပီဆုိတာနဲ႕ ေအာက္ကလိုေပၚလာပါမယ္။ Version ေပၚမူတည္ျပီး ပံုစံအနည္းငယ္ကြာျခားမႈ
ရွိပါမယ္။



Form ေပၚကုိ ျမွားခ်လုိက္တာနဲ႕ ၾကိဳက္ရာ Form Name ကုိေရးေပးပါ။
ဒီမွာေတာ့ Project Name နဲ႕ တူေအာင္ Android One-Click Tutorial လုိ႕ပဲေပးထားပါတယ္။
Form ကုိ လုိသလုိ အက်ဥ္းအက်ယ္ ခ်ဲ႕ျပီး ေအာက္ကလုိ ပံုေလးျဖစ္ေအာင္ထားလုိက္ပါ။


ျပီးရင္ေတာ့ Program Run လုိ႕အဆင္ေျပမေျပ စမ္းၾကည့္ပါ။ F5 ကုိႏွိပ္ျပီး



အဆင္ေျပရင္ေတာ့ ADB ဖုိင္ေလးေတြထည့္ပါမယ္။
ADB Bundle ကုိ ဒီမွာေဒါင္းပါ


ေဒါင္းလုိ႕ရတာေတြကုိ Computer ရဲ႕ documents folder ထဲမွာထားလုိက္ပါ။
'adb.exe', 'AdbWinApi.dll', 'AdbWinUsbApi.dll' and 'fastboot.exe' ဖုိင္ေလးဖုိင္ကုိသံုးမွာပါ
ဖိုင္ေလးဖုိင္ပါတာစံုတယ္ဆုိ  Visual Studio ရဲ႕ Project properties ကုိဝင္ပါ။  (Project ->> <Project Name> properties)  'Resources' ကိုေရြးပါ။ String format ျဖစ္ေနပါလိမ့္မယ္။ files ကုိေျပာင္းေပးပါ။



အေပၚက adb file ေလးခုကုိ add ေပးပါ။ ေအာက္ကလုိမ်ိဳးပါ။


အားလံုးျပီးသြားရင္ coding ပုိင္းစေရးလို႕ရပါျပီ။



Form ေပၚကုိ Double Click ႏွိပ္လုိက္ရင္ coding tab ေပၚလာပါမယ္ ။ အေပၚဆံုးမွာ ေအာက္က
Code ကိုျဖည့္ပါ။

Code:
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms.DialogResult

adb file ေတြကုိ Copy ယူဖုိ႕ Coding ေတြထည့္ပါမယ္။

ေအာက္မွာေပးထားသလုိျဖည့္ေပးပါ။
Code:
If Not Directory.Exists("ADB") Then
            Directory.CreateDirectory("ADB")
        Else
            If Not File.Exists("ADB\adb.exe") Then
                File.WriteAllBytes("ADB\adb.exe", My.Resources.adb)
            End If
            If Not File.Exists("ADB\AdbWinApi.dll") Then
                File.WriteAllBytes("ADB\AdbWinApi.dll", My.Resources.AdbWinApi)
            End If
            If Not File.Exists("ADB\AdbWinUsbApi.dll") Then
                File.WriteAllBytes("ADB\AdbWinUsbApi.dll", My.Resources.AdbWinUsbApi)
            End If
            If Not File.Exists("ADB\fastboot.exe") Then
                File.WriteAllBytes("ADB\fastboot.exe", My.Resources.fastboot)
            End If
        End If
အေပၚကေပးထားတဲ့ Code ေတြျဖည့္တဲ့အခါ အခုျပထားသလုိျဖစ္ေနရပါမယ္။




ေနာက္တစ္ဆင့္သြားပါမယ္။


ခလုတ္ေလးခုထည့္ပါမယ္။ေအာက္ကလုိ နာမည္ေပးပါ။

  • Back up the device
  • Restore the device
  • Install an app
  • Push a file
Adding Buttons and Commands:
Move back to the designer and add a few buttons like I've done. The buttons I've created will: Now, we want to create four more forms. One for the backup, one for the restore, one for the install app and one for pushing a file. Hit CTRL+SHIFT+A to add new items. You can name the forms however you want. I created some with pretty self-explaining names: Now, double-click on each button in Form1 to create a new code block in the code file. Once you have done that, copy the following codes into each code block. Button1_ClickButton2_ClickButton3_ClickButton4_Click Now open up the Backup form. We'll start here. You can close the Form1-files. Start designing the form as you wish. Here's how I've done it: If you're using the same design as me, you might want to use the same code. This is working code. I have debugged and tested!Once you have that done, move to the next form. This, in my case, is Restore. To keep the thread clear, I'll carry on in post #2.













Code:
Backup.Show()
Me.Hide()

Code:
Restore.Show()
Me.Hide()

Code:
Install.Show()
Me.Hide()

Code:
Push.Show()
Me.Hide()







NOTE: I rarely use the .Net components in the Toolbox. Only for static operations. For things like dialog boxes, I use pure code.
Code:
Imports System.IO
Public Class Backup

    Private Sub Backup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox2.Text = "Backup_From_" & Date.Now.ToShortTimeString
        If Not Directory.Exists(TextBox1.Text) Then
            Directory.CreateDirectory(TextBox1.Text)
        End If
    End Sub

    Private Sub Backup_FormClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        Form1.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FolderBrowse As New FolderBrowserDialog
        FolderBrowse.Description = "Select the destination of where you wish your backup to be saved to." _
            & "Note: Please do not choose locations with spaces in the directories. These may cause errors!"
        FolderBrowse.ShowNewFolderButton = True
        Dim DialogRes As DialogResult = FolderBrowse.ShowDialog
        If DialogRes = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = FolderBrowse.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Shell("""ADB\adb.exe"" backup -f" & TextBox1.Text & "\" & TextBox2.Text & "-apk -system -full -all", AppWinStyle.NormalFocus, True, 30000)
    End Sub
End Class


Ok, now let's get on with Restore.

Open up the file, and again, design it as you want.
If you're using the same design as me, it is advisory, that you use the same code.


Here is the code I used:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim OpenFile As New OpenFileDialog
        OpenFile.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        OpenFile.Multiselect = False
        OpenFile.Filter = "AB (Android Backups)|*.ab"
        OpenFile.SupportMultiDottedExtensions = False
        OpenFile.Title = "Select the Android Backup (*.ab) file to restore your device from..."
        Dim DialogRes As DialogResult = OpenFile.ShowDialog()
        If DialogRes = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = OpenFile.FileName
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Shell("""ADB\adb.exe"" restore " & TextBox1.Text, AppWinStyle.NormalFocus, True, 30000)
    End Sub

    Private Sub Restore_FormClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        Form1.Show()
    End Sub
And now we're ready to move to the third form. As usual; if you're using the same design as me, you'll want to use the same code as me.
I'd like to note: I'll explain all the code in post #3.

The third form (Install an App) will be a bit different than the others. Here, we'll give the user the opportunity to select an entire folder which contains .apk files and then with a mouse-click, the app will install the desired APK.


Note the ListBox, That is where all the APKs will be listed. (Hence the name 'ListBox'.)

I have pulled some APKs from my phone and have put them in a folder (C:\APKs). We will use this folder to list all the available APKs in the listbox.
But before we do that, here is the code for the form. Again, nothing is imported here.
Code:
Private Sub Install_FormClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        Form1.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FolderBrowse As New FolderBrowserDialog
        FolderBrowse.Description = "Select the folder containing your APK files."
        FolderBrowse.RootFolder = Environment.SpecialFolder.DesktopDirectory
        FolderBrowse.ShowNewFolderButton = False
        Dim DialogRes As DialogResult = FolderBrowse.ShowDialog()
        If DialogRes = Windows.Forms.DialogResult.OK Then
            For Each Item As String In My.Computer.FileSystem.GetFiles(FolderBrowse.SelectedPath)
                ListBox1.Items.Add(Item)
            Next
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Shell("""ADB\adb.exe"" install " & ListBox1.SelectedItem.ToString, AppWinStyle.NormalFocus, True, 30000)
    End Sub
And here are some pictures of the code in action:

FolderBrowserDialog (FolderBrowse):


The list of apps (ListBox):


Ok. We're almost done with our One-Click utility!

We've only got one more form and we'll do that in a dash! Then I'll get to explaining what everything means. Though most of it is pretty much self-explanatory, I'd rather go over it.

Move on to the last form, and the same rules apply.

This form will be using the same method as the Install form - Using a ListBox to display files.


Here is the code:
Code:
Private Sub Push_FormClosing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        Form1.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FolderBrowse As New FolderBrowserDialog
        FolderBrowse.Description = "Select the folder containing the file/s you want to push to the device..."
        FolderBrowse.ShowNewFolderButton = False
        FolderBrowse.RootFolder = Environment.SpecialFolder.DesktopDirectory
        Dim DialogRes As DialogResult = FolderBrowse.ShowDialog()
        If DialogRes = Windows.Forms.DialogResult.OK Then
            For Each Item As String In My.Computer.FileSystem.GetFiles(FolderBrowse.SelectedPath)
                ListBox1.Items.Add(Item)
            Next
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Shell("""ADB\adb.exe"" push " & ListBox1.SelectedItem & " " & TextBox1.Text, AppWinStyle.NormalFocus, True, 30000)
    End Sub
Cool! We've got our first One-Click-Utility done in Visual Basic.Net! This is pretty awesome, don't you think? I may have to re-do this thread, but for the moment it'll do, I guess.

Move down to the third post, to read all about what what piece of code does.

0 comments:

Post a Comment