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.content.SharedPreferences; 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; private BroadcastReceiver receiver; /** * Hold the shared preferences for the app */ private SharedPreferences mSharedPreferences; public void portbinderAlert(final SharedPreferences.Editor editor){ 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.no_thanks, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //never show this to users again if (editor != null) { editor.putBoolean("donotshowagain", true); editor.commit(); //commit if any changes on the preferences } } } ) .setNeutralButton(R.string.how, 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) { } }) .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) { dialog.dismiss(); new AlertDialog.Builder(getActivity()) .setTitle(R.string.portbinder) .setMessage(R.string.confirm_msg) .setPositiveButton(R.string.confirm, 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 String fileName = ""; //selecting necessary PortBinder architecture if (arch.matches("arm.*")) //arm fileName = "bind-arm.zip"; else if (arch.matches("x86")) //x86 fileName = "bind-x86.zip"; else if (arch.matches("mips")) //mips fileName = "bind-mips.zip"; Uri uri = Uri.parse("https://www.tk.informatik.tu-darmstadt.de/fileadmin/user_upload/Group_TK/"+fileName); if (!fileName.isEmpty()) //As long we have a valid string (non-empty) { Request request = new Request(uri) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); enqueue = dm.enqueue(request); } else { //report to user of an unknown architecture } } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setIcon(android.R.drawable.ic_dialog_alert).show(); ; } }) .setIcon(android.R.drawable.ic_dialog_info).show(); } }) .setIcon(android.R.drawable.ic_dialog_info).show(); } @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); mSharedPreferences = getActivity().getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE); final SharedPreferences.Editor editor = mSharedPreferences.edit(); 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 { if (!mSharedPreferences.getBoolean("donotshowagain", false)) portbinderAlert(editor); porthackText.setText(R.string.no); porthackText.setTextColor(getResources().getColor(R.color.holo_red)); } //Handle if a download 'may' be needed 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)); 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(); } /** * {@inheritDoc} */ @Override public void onDestroy() { super.onDestroy(); getActivity().unregisterReceiver(receiver); } }