123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import android.app.AlertDialog;
- import android.app.DownloadManager;
- import android.app.DownloadManager.Query;
- import android.app.DownloadManager.Request;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.channels.FileChannel;
- import java.util.zip.ZipInputStream;
- import de.tudarmstadt.informatik.hostage.Hostage;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.system.Decompress;
- import de.tudarmstadt.informatik.hostage.system.Device;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- /**
- * Creates the view to edit the preferences of the app and shows the porthack and rooted state of the device
- *
- * @author Alexander Brakowski
- * @created 24.02.14 23:37
- */
- public class SettingsFragment extends UpNavigatibleFragment {
- /**
- * {@inheritDoc}
- */
- private long enqueue;
- private DownloadManager dm;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- getActivity().setTitle(getResources().getString(R.string.drawer_settings));
- View v = inflater.inflate(R.layout.fragment_settings, container, false);
- final TextView rootedText = (TextView) v.findViewById(R.id.settings_device_rooted);
- final TextView porthackText = (TextView) v.findViewById(R.id.settings_porthack_installed);
- if (Device.isRooted()) {
- rootedText.setText(R.string.yes);
- rootedText.setTextColor(getResources().getColor(R.color.holo_dark_green));
- } else {
- rootedText.setText(R.string.no);
- rootedText.setTextColor(getResources().getColor(R.color.holo_red));
- }
- if (Device.isPorthackInstalled()) {
- porthackText.setText(R.string.yes);
- porthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
- } else {
- porthackText.setText(R.string.no);
- porthackText.setTextColor(getResources().getColor(R.color.holo_red));
- }
- //Handle if a download 'may' be needed
- BroadcastReceiver receiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
- long downloadId = intent.getLongExtra(
- DownloadManager.EXTRA_DOWNLOAD_ID, 0);
- Query query = new Query();
- query.setFilterById(enqueue);
- Cursor c = dm.query(query);
- if (c.moveToFirst()) {
- int columnIndex = c
- .getColumnIndex(DownloadManager.COLUMN_STATUS);
- if (DownloadManager.STATUS_SUCCESSFUL == c
- .getInt(columnIndex)) {
- String downloadFile = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
- String makeWritable[]= {"su","-c","chmod","777","/data/local"}; //change permission to allow rw access from x only
- String allowExec[]= {"su","-c","chmod","711","/data/local/bind"}; //change permission to allow x access
- String revert[]= {"su","-c","chmod","751","/data/local"}; //change permission back to x only
- try {
- //Chmod the local directory for write access
- Process process = Runtime.getRuntime().exec(makeWritable);
- process.waitFor();
- Log.d("portbinder:","Changing permission on /data/local to allow write access");
- //Decompressing downloaded zip to local directory
- Decompress dwnld = new Decompress(downloadFile,"/data/local/" );
- dwnld.unzip();
- Log.v("portbinder:","Decompressing downloaded file");
- //Chmod the Portbinder to allow it to be executable
- process = Runtime.getRuntime().exec(allowExec);
- process.waitFor();
- Log.v("portbinder:","Changing permission on /data/local/bind to allow the binary to be executed");
- //Chmod the local directory to back to non-read/write access
- process = Runtime.getRuntime().exec(revert);
- process.waitFor();
- Log.v("portbinder:","Changing permission on /data/local back to no-write access");
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if(Device.updatePorthack()) //if successful
- {
- porthackText.setText(R.string.yes);
- porthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
- }
- }
- }
- }
- }
- };
- getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- //Allow no porthack text box clickable
- if (Device.isRooted() && !Device.isPorthackInstalled())
- {
- porthackText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- new AlertDialog.Builder(getActivity())
- .setTitle(R.string.information)
- .setMessage(R.string.no_portbinder_msg)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .setNegativeButton(R.string.help, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- final AlertDialog alert;
- new AlertDialog.Builder(getActivity())
- .setTitle(R.string.portbinder)
- .setMessage(R.string.helpPortbinder)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- /*.setNeutralButton(R.string.portbinder_tutorial, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Uri uri = Uri.parse("https://www.youtube.com/watch?v=gi6fSiIJASk");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- })*/
- .setNegativeButton(R.string.portbinder_website, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Uri uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/de/research/secure-smart-infrastructures/hostage/");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- })
- //Testing automated installation of portbinder
- .setNeutralButton(R.string.help_me, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- //Download Portbinder
- dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
- //Identify architecture
- String arch = System.getProperty("os.arch"); //get the device architecture
- //selecting necessary PortBinder
- Uri uri = Uri.EMPTY; //initialize variable
- if (arch.matches("arm.*")) //arm
- uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-arm.zip");
- else if (arch.matches("x86")) //x86
- uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-x86.zip");
- else if (arch.matches("mips")) //mips
- uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/bind-mips.zip");
- if (uri != Uri.EMPTY) {
- Request request = new Request(uri)
- .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "bind.zip");
- enqueue = dm.enqueue(request);
- // dm.enqueue(request);
- }
- else {
- //report to user of an unknown architecture
- }
- }
- })
- .setIcon(android.R.drawable.ic_dialog_info).show();
- }
- })
- .setIcon(android.R.drawable.ic_dialog_info).show();}
- });
- }
- return v;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- FragmentManager manager = this.getFragmentManager();
- manager.beginTransaction().replace(R.id.settings_fragment_container, new PreferenceHostageFrament()).commit();
- }
- }
|