I'm very new to Android so maybe my question is a bit of a beginner. I want to use the futureTask method in my program, but I really do not know how to use it. I have a sample code of this method and I want to combine it with my own code. onActivityResult()
is my code that I want to process in the background. I tried to embed the code in the sample code instead of call()
but got an error
this is sample code
:
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
public static Future<String> startComputation() throws IOException {
return pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.currentThread().sleep(3000);
return "finish computation";
}
});
}
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
final Future<String> contentsFuture = startComputation();
while (!contentsFuture.isDone()) {
System.out.println("computation in progress...");
System.out.println("do another work");
Thread.currentThread().sleep(1000);
}
System.out.println(contentsFuture.get());
System.exit(0);
}
and my MainAvtivity
code:
package com.example.imen_yar;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.chaquo.python.PyException;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
ImageButton b_main;
Button b_link;
TextView text;
String interpreterOutput = "";
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_main = findViewById(R.id.b_main);
b_link = findViewById(R.id.b_link);
text = findViewById(R.id.textView);
b_link.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, MainActivity3.class);
startActivity(intent);
});
NavigationView nvDrawer = findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
// To run Python Interpreter
if(!Python.isStarted()){
Python.start(new AndroidPlatform(this));
}
b_main.setOnClickListener(view ->{
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
});
}
@SuppressLint("SetTextI18n")
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
Uri uri = data.getData();
Context context = this;
String path = RealPathUtil.getRealPath(context,uri);
if(path != null){
Python py = Python.getInstance();
String code = "import vt\n";
code += "import hashlib\n";
code += "import time\n";
code += "client = vt.Client(\"e9481497c3198559e299e966ec79be2ece56bf903cd0308474f8e3059aad6ee5\")\n";
code += "def check():\n";
code += " try:\n";
code += " with open(r\"" + path + "\",\"rb\") as f:\n";
code += " bytes = f.read() # read the entire file as bytes\n";
code += " readable_hash = hashlib.sha256(bytes).hexdigest()\n";
code += " file = client.get_object(\"/files/\" + readable_hash)\n";
code += " x = str(file.last_analysis_stats)\n";
code += " n = 109\n";
code += " if x[n] + x[n+1] + x[n+2] + x[n+3] + x[n+4] + x[n+5] + x[n+6] + x[n+7] + x[n+8] == \"malicious\":\n";
code += " t = x[n + 12]\n";
code += " elif x[n+1] + x[n+2] + x[n+3] + x[n+4] + x[n+5] + x[n+6] + x[n+7] + x[n+8] + x[n+9] == \"malicious\":\n";
code += " t = x[n + 13]\n";
code += " elif x[n+2] + x[n+3] + x[n+4] + x[n+5] + x[n+6] + x[n+7] + x[n+8] + x[n+9] + x[n+10] == \"malicious\":\n";
code += " t = x[n + 14]\n";
code += " elif x[n+3] + x[n+4] + x[n+5] + x[n+6] + x[n+7] + x[n+8] + x[n+9] + x[n+10] + x[n+11] == \"malicious\":\n";
code += " t = x[n + 15]\n";
code += " elif x[n+4] + x[n+5] + x[n+6] + x[n+7] + x[n+8] + x[n+9] + x[n+10] + x[n+11] + x[n+12] == \"malicious\":\n";
code += " t = x[n + 16]\n";
code += " elif x[n+5] + x[n+6] + x[n+7] + x[n+8] + x[n+9] + x[n+10] + x[n+11] + x[n+12] + x[n+13] == \"malicious\":\n";
code += " t = x[n + 17]\n";
code += " elif x[n+6] + x[n+7] + x[n+8] + x[n+9] + x[n+10] + x[n+11] + x[n+12] + x[n+13] + x[n+14] == \"malicious\":\n";
code += " t = x[n + 18]\n";
code += " if t == \"0\":\n";
code += " print(\"file is safe\")\n";
code += " else:\n";
code += " print(\"file isn't safe\")\n";
code += " except:\n";
code += " #print(\"oh. it's a new file!!\")\n";
code += " check2()\n";
code += "def check2(): \n";
code += " with open(r\"" + path + "\", \"rb\") as f:\n";
code += " analysis = client.scan_file(f, wait_for_completion=True)\n";
code += " while True:\n";
code += " print(\"checking\")\n";
code += " analysis = client.get_object(\"/analyses/{}\", analysis.id)\n";
code += " #print(analysis.status)\n";
code += " if analysis.status == \"completed\":\n";
code += " check()\n";
code += " break\n";
code += " time.sleep(10)\n";
code += "check()\n";
PyObject sys = py.getModule("sys");
PyObject io = py.getModule("io");
PyObject console = py.getModule("interpreter");
PyObject textOutputStream = io.callAttr("StringIO");
sys.put("stdout", textOutputStream);
try {
console.callAttrThrows("mainTextCode", code);
interpreterOutput = String.valueOf(textOutputStream.callAttr("getvalue"));
}catch (PyException e){
interpreterOutput = e.getMessage();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
String output_one = "safe\n";
output_one += "Unclosed client session\n";
output_one += "client_session: <aiohttp.client.ClientSession object at 0x00000252B05A5F40>\n";
output_one += "Unclosed connector\n";
output_one += "connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x00000252B05D3640>, 243493.796)]']\n";
output_one += "connector: <aiohttp.connector.TCPConnector object at 0x00000252ADC3CA90>";
String output_two = "isn't safe\n";
output_two += "Unclosed client session\n";
output_two += "client_session: <aiohttp.client.ClientSession object at 0x00000252B05A5F40>\n";
output_two += "Unclosed connector\n";
output_two += "connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x00000252B05D3640>, 243493.796)]']\n";
output_two += "connector: <aiohttp.connector.TCPConnector object at 0x00000252ADC3CA90>";
if (interpreterOutput != null) {
if(interpreterOutput.equals(output_one)){
System.out.println("file is safe");
text.setText("safe");
}else if(interpreterOutput.equals(output_two)){
System.out.println("file isn't safe!");
text.setText("isn't safe!");
}
}
///System.out.println(interpreterOutput);
//text.setText("not run");
}
}
}
}
//salam
can you help me?