fixed upload file, added page to view blockchain and remove from blockchain

This commit is contained in:
2022-01-02 00:31:54 +04:00
parent f3fe02eb5b
commit eacc9dac76
17 changed files with 301 additions and 39 deletions

2
.idea/misc.xml generated
View File

@@ -3,9 +3,11 @@
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="../../Android/Sdk/platforms/android-31/data/res/layout/simple_list_item_1.xml" value="0.3319746376811594" />
<entry key="app/src/main/res/drawable-v24/ic_launcher_foreground.xml" value="0.37864583333333335" />
<entry key="app/src/main/res/drawable/ic_launcher_background.xml" value="0.37864583333333335" />
<entry key="app/src/main/res/drawable/peernet_logo_black.xml" value="0.37864583333333335" />
<entry key="app/src/main/res/layout/activity_blockchain_info.xml" value="0.3319746376811594" />
<entry key="app/src/main/res/layout/activity_main.xml" value="0.33" />
<entry key="app/src/main/res/layout/activity_search.xml" value="0.3625" />
<entry key="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" value="0.37864583333333335" />

View File

@@ -29,6 +29,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
@@ -38,6 +41,8 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.android.volley:volley:1.2.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

Binary file not shown.

View File

@@ -16,12 +16,19 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Test"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true" >
android:usesCleartextTraffic="true">
<activity
android:name=".Blockchain_info"
android:exported="true" />
<activity
android:name=".blockchainInfo"
android:exported="true"
android:label="@string/title_activity_blockchain_info"
android:theme="@style/Theme.Test.NoActionBar" />
<activity
android:name=".Search"
android:exported="true" />

View File

@@ -0,0 +1,146 @@
package com.peernet.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONObject
import org.json.JSONTokener
import java.util.*
class Blockchain_info : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_blockchain_info)
val queue = Volley.newRequestQueue(this)
val url = "http://127.0.0.1:5125/blockchain/file/list"
var ResponseObjectSearch: JSONObject
ResponseObjectSearch = JSONObject("{\"keyword\": null}");
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
ResponseObjectSearch = JSONTokener(response.toString()).nextValue() as JSONObject
val blockchainResult = findViewById<ListView>(R.id.SearchResult)
val listItems = arrayOfNulls<String>(ResponseObjectSearch.getJSONArray("files").length())
for (i in 0 until ResponseObjectSearch.getJSONArray("files").length()) {
val searchResultElement = ResponseObjectSearch.getJSONArray("files")[i]
val ResponseObjectInLoop = JSONTokener(ResponseObjectSearch.getJSONArray("files")[i].toString()).nextValue() as JSONObject
listItems[i] = ResponseObjectInLoop.get("name").toString()
}
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems)
blockchainResult.adapter = adapter
},
Response.ErrorListener { errorresponse ->
// val connectionLabel1 = findViewById<View>(R.id.PeernetInfo1) as TextView
// connectionLabel1.text = errorresponse.toString()
Log.d("myTag", errorresponse.toString());
})
queue.add(stringRequest)
val blockchainResult = findViewById<ListView>(R.id.SearchResult)
blockchainResult.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
val searchResultElement = ResponseObjectSearch.getJSONArray("files")[position]
val FileName = JSONTokener(ResponseObjectSearch.getJSONArray("files")[position].toString()).nextValue() as JSONObject
// // Remove file warehouse
// val urlRemoveFileWarehouse = "http://127.0.0.1:5125/warehouse/delete?hash=" + FileName.get("hash")
//
// // Request a string response from the provided URL.
// val stringRequest1 = StringRequest(
// Request.Method.GET, urlRemoveFileWarehouse,
// Response.Listener<String> { response ->
// //val connectionLabel = findViewById<View>(R.id.PeernetInfo) as TextView
// // Display the first 500 characters of the response string.
// // textView.text = "Response is: ${response.substring(0, 500)}"
// //connectionLabel.text= "Response is: ${response.substring(0, 500)}"
// Log.d("myTag", "Response is: ${response}");
// },
// Response.ErrorListener { errorresponse ->
//// val connectionLabel1 = findViewById<View>(R.id.PeernetInfo1) as TextView
//// connectionLabel1.text = errorresponse.toString()
// Log.d("myTag", errorresponse.toString());
// })
//
//// Add the request to the RequestQueue.
// queue.add(stringRequest1)
// Remove file from blockchain
val urlRemoveBlockChain = "http://127.0.0.1:5125/blockchain/file/delete"
val jsonobj = JSONObject()
jsonobj.put("id", FileName.get("id"))
val jsonArrayObj = JSONArray()
val JSONPostRequest = JSONObject()
jsonArrayObj.put(0,jsonobj)
JSONPostRequest.put("files",jsonArrayObj)
Log.d("myTag", JSONPostRequest.toString())
// remove file meta-data / information from the blockchain
val stringRequest = JsonObjectRequest(
Request.Method.POST,urlRemoveBlockChain,JSONPostRequest,
Response.Listener { response ->
//val connectionLabel = findViewById<View>(R.id.PeernetInfo) as TextView
// Display the first 500 characters of the response string.
// textView.text = "Response is: ${response.substring(0, 500)}"
//connectionLabel.text= "Response is: ${response.substring(0, 500)}"
// val connectionLabel1 = findViewById<View>(R.id.PeernetInfo1) as TextView
// connectionLabel1.text = response.toString()
Log.d("RemoveBlockchain", "Response is: ${response}");
// val ResponseObject = JSONTokener(response.toString()).nextValue() as JSONObject
},
Response.ErrorListener { errorresponse ->
// val connectionLabel1 = findViewById<View>(R.id.PeernetInfo1) as TextView
// connectionLabel1.text = errorresponse.toString()
Log.d("myTag", errorresponse.toString());
})
queue.add(stringRequest)
finish();
startActivity(getIntent());
}
}
}

View File

@@ -45,6 +45,7 @@ import org.json.JSONTokener
import android.net.NetworkInfo
import android.net.ConnectivityManager
import android.os.Handler
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.net.toFile
@@ -55,9 +56,6 @@ import java.nio.file.Paths
class MainActivity : AppCompatActivity() {
//private val client = OkHttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -125,12 +123,12 @@ class MainActivity : AppCompatActivity() {
isConnected.text = ResponseObject.get("isconnected").toString()
numPeers.text = ResponseObject.get("countpeerlist").toString()
// connectionLabel.text = response.toString()
// connectionLabel.text = response.toString()
// Log.d("myTag", "Response is: ${response.substring(0, 500)}");
},
Response.ErrorListener { errorresponse ->
//val connectionLabel = findViewById<View>(R.id.PeernetInfo) as TextView
// connectionLabel.text = errorresponse.toString()
// connectionLabel.text = errorresponse.toString()
Log.d("myTag", errorresponse.toString());
})
@@ -147,7 +145,7 @@ class MainActivity : AppCompatActivity() {
//connectionLabel.text= "Response is: ${response.substring(0, 500)}"
// val connectionLabel = findViewById<View>(R.id.PeernetInfo2) as TextView
// connectionLabel.text = response.toString()
// Log.d("myTag", "Response is: ${response.substring(0, 500)}");
// Log.d("myTag", "Response is: ${response.substring(0, 500)}");
},
Response.ErrorListener { errorresponse ->
// val connectionLabel = findViewById<View>(R.id.PeernetInfo2) as TextView
@@ -179,6 +177,14 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}
// open files added in the local blockchain
val FilesAdded = findViewById<Button>(R.id.FIlesAdded)
FilesAdded.setOnClickListener {
val intent = Intent(this, Blockchain_info::class.java)
startActivity(intent)
}
@@ -190,7 +196,7 @@ class MainActivity : AppCompatActivity() {
// run("http://127.0.0.1:5125/status")
// requestAppPermissions();
// requestAppPermissions();
//Mobilecore.mobileCoreStart()
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@@ -238,7 +244,7 @@ class MainActivity : AppCompatActivity() {
val path = FileUtils.getPath(this, data?.data)
Log.d("path",path.toString())
Log.d("path",path.substring(0, path.lastIndexOf('/')))
// convert Path string to URI
val pathUri = path.toUri()
@@ -246,18 +252,18 @@ class MainActivity : AppCompatActivity() {
// convert file to bytes
var encoded: ByteArray
// try {
encoded = Files.readAllBytes(Paths.get(path))
Log.d("bytes", encoded.toString())
// try {
encoded = Files.readAllBytes(Paths.get(path))
Log.d("bytes", encoded.toString())
// } catch (e: IOException) {
// Log.d("bytes", e.toString())
// }
// data?.data
// data?.data
// imageFile = new File(getRealPathFromURI(selectedImageURI));
// imageFile = new File(getRealPathFromURI(selectedImageURI));
// filePath = File((data?.data?.let { getRealPathFromURI(it) })).toString()
// filePath = File((data?.data?.let { getRealPathFromURI(it) })).toString()
// var file = File(
// Environment.getExternalStorageDirectory().absolutePath,
@@ -304,7 +310,7 @@ class MainActivity : AppCompatActivity() {
//connectionLabel.text= "Response is: ${response.substring(0, 500)}"
// val connectionLabel = findViewById<View>(R.id.PeernetInfo2) as TextView
// connectionLabel.text = response.toString()
Log.d("myTag", "Response is: ${response.toString()}");
Log.d("myTag", "Response is: ${response.toString()}");
// read hash from the json object and add to the blockchain
val ResponseObject = JSONTokener(response.toString()).nextValue() as JSONObject
@@ -318,9 +324,9 @@ class MainActivity : AppCompatActivity() {
jsonobj.put("hash", ResponseObject.get("hash"))
jsonobj.put("type", 5)
jsonobj.put("format", 1)
jsonobj.put("size", encoded.size.toString())
jsonobj.put("size", encoded.size)
jsonobj.put("name", path.substring(path.lastIndexOf("/")+1))
jsonobj.put("folder", path)
jsonobj.put("folder", path.substring(0, path.lastIndexOf('/')))
jsonobj.put("description", "")
val jsonArrayObj = JSONArray()
@@ -348,7 +354,7 @@ class MainActivity : AppCompatActivity() {
// connectionLabel1.text = response.toString()
Log.d("myTag1", "Response is: ${response}");
// val ResponseObject = JSONTokener(response.toString()).nextValue() as JSONObject
// val ResponseObject = JSONTokener(response.toString()).nextValue() as JSONObject
@@ -423,7 +429,7 @@ class MainActivity : AppCompatActivity() {
}
if (hasReadPermissions() && hasWritePermissions()) {
// connectionLabel.text = "lol123"
// connectionLabel.text = "lol123"
return
}
@@ -454,5 +460,4 @@ class MainActivity : AppCompatActivity() {
}

View File

@@ -1,14 +1,13 @@
package com.peernet.test
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.telephony.mbms.DownloadRequest
import android.util.Log
import android.view.View
import android.widget.*
import androidx.annotation.RequiresApi
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
@@ -16,9 +15,11 @@ import com.android.volley.toolbox.Volley
import com.android.volley.toolbox.JsonObjectRequest
import org.json.JSONObject
import org.json.JSONTokener
import java.util.*
class Search : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
@@ -210,13 +211,18 @@ class Search : AppCompatActivity() {
val NodeID = FileName.get("nodeid").toString()
Log.d("myTag", NodeID)
// Get download path for the android phone
val DownloadPath = "/data/local/tmp"
// decode base64 file hash
val FileHashByte: ByteArray = Base64.getDecoder().decode(FileHash)
Log.d("download path", DownloadPath.toString());
val DownloadPath = this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
Log.d("download", DownloadPath.toString());
// Get request URL to download the file
val DownloadURL = "http://127.0.0.1:5125/download/start?path=test.txt" + "&hash=" + FileHash + "&node=" + NodeID
val DownloadURL = "http://127.0.0.1:5125/download/start?path=" + java.net.URLEncoder.encode(DownloadPath.toString() + "/" + FileName.get("name"), "utf-8") + "&hash=" + java.net.URLEncoder.encode(FileHash, "utf-8") + "&node=" + java.net.URLEncoder.encode(NodeID, "utf-8")
val DownloadRequest = StringRequest(
Request.Method.GET, DownloadURL,
@@ -227,7 +233,7 @@ class Search : AppCompatActivity() {
//connectionLabel.text= "Response is: ${response.substring(0, 500)}"
// val connectionLabel = findViewById<View>(R.id.PeernetInfo) as TextView
// connectionLabel.text = response.toString()
Log.d("myTag", "Response is: ${response.substring(0, 500)}");
Log.d("myTag", "Response is: ${response}");
},
Response.ErrorListener { errorresponse ->
// val connectionLabel = findViewById<View>(R.id.PeernetInfo) as TextView
@@ -250,4 +256,7 @@ class Search : AppCompatActivity() {
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Blockchain_info">
<ListView
android:id="@+id/SearchResult"
android:layout_width="405dp"
android:layout_height="727dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.417"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.015" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -34,13 +34,24 @@
<Button
android:id="@+id/AddFIle"
android:layout_width="129dp"
android:layout_height="66dp"
android:layout_marginBottom="12dp"
android:text="Add file "
android:layout_width="79dp"
android:layout_height="44dp"
android:layout_marginBottom="24dp"
android:text="+"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.606"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/FIlesAdded"
android:layout_width="134dp"
android:layout_height="70dp"
android:layout_marginBottom="12dp"
android:text="Files Added "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.978"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
@@ -62,10 +73,10 @@
android:textColorLink="#1E1D1D"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.061"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
app:layout_constraintVertical_bias="0.977" />
<TextView
android:id="@+id/Description"

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="com.peernet.test.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="com.peernet.test.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
<action
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
</navigation>

View File

@@ -0,0 +1,3 @@
<resources>
<dimen name="fab_margin">48dp</dimen>
</resources>

View File

@@ -0,0 +1,3 @@
<resources>
<dimen name="fab_margin">200dp</dimen>
</resources>

View File

@@ -0,0 +1,3 @@
<resources>
<dimen name="fab_margin">48dp</dimen>
</resources>

View File

@@ -0,0 +1,3 @@
<resources>
<dimen name="fab_margin">16dp</dimen>
</resources>

View File

@@ -1,3 +1,12 @@
<resources>
<string name="app_name">test</string>
<string name="title_activity_blockchain_info">blockchainInfo</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
</resources>

View File

@@ -13,4 +13,13 @@
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.Test.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.Test.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.Test.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

View File

@@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files