Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. package com.example.kash.postgresql;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.preference.PreferenceManager;
  10. import android.support.v7.app.ActionBarActivity;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.ListView;
  18. import android.widget.Toast;
  19.  
  20. import java.io.File;
  21. import java.lang.reflect.Array;
  22. import java.util.ArrayList;
  23.  
  24.  
  25. public class MainActivity extends ActionBarActivity
  26. {
  27.     String user,url,DB,password;
  28.     int port;
  29.     ListView lvProfiles;
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState)
  32.     {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_main);
  35.         lvProfiles= (ListView)findViewById(R.id.lvProfiles);
  36.         populateList();
  37.     }
  38.  
  39.     public void populateList() {
  40.         File prefsdir=new File(getApplicationInfo().dataDir,"shared_prefs");
  41.         if(prefsdir.exists() && prefsdir.isDirectory())
  42.         {
  43.             final String[] profiles=prefsdir.list();
  44.             final ArrayList<String> list = new ArrayList<>();
  45.             for(int i=0;i<profiles.length;i++)
  46.             {
  47.                 list.add(profiles[i].replaceFirst(".xml",""));
  48.             }
  49.             list.remove("com.example.kash.postgresql_preferences");
  50.             for (String s : list)
  51.             {
  52.                 if(s.endsWith(".bak"))
  53.                     list.remove(s);
  54.             }
  55.             final ArrayAdapter<String> adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
  56.             lvProfiles.setAdapter(adapter);
  57.             lvProfiles.setClickable(true);
  58.             lvProfiles.setLongClickable(true);
  59.             lvProfiles.setOnItemClickListener(new AdapterView.OnItemClickListener()
  60.             {
  61.                 @Override
  62.                 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  63.                 {
  64.                     String profile = adapter.getItem(position);
  65.                     profile = profile.replaceFirst(".xml", "");
  66.                     Log.d("onItemClick", "Profile " + profile + " selected");
  67.                     //openTextual(profile);
  68.                     openGraphical(profile);
  69.                 }
  70.             });
  71.             lvProfiles.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
  72.             {
  73.                 @Override
  74.                 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
  75.                 {
  76.                     final String profile = adapter.getItem(position);
  77.                     AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
  78.                     builder1.setMessage("Delete profile " + profile+" ?");
  79.                     builder1.setCancelable(true);
  80.                     builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener()
  81.                     {
  82.                         public void onClick(DialogInterface dialog, int id)
  83.                         {
  84.                             File file = new File(getFilesDir().getPath()+"/../shared_prefs/"+profile+".xml");
  85.                             Log.d("",file.toString());
  86.                             if (file.delete())
  87.                             {
  88.                                 Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
  89.                                 populateList();
  90.                             }
  91.                             else
  92.                             {
  93.                                 Toast.makeText(getApplicationContext(), "Not deleted", Toast.LENGTH_SHORT).show();
  94.                             }
  95.                             dialog.cancel();
  96.                         }
  97.                     });
  98.                     builder1.setNegativeButton("No", new DialogInterface.OnClickListener()
  99.                     {
  100.                         public void onClick(DialogInterface dialog, int id)
  101.                         {
  102.                             dialog.cancel();
  103.                         }
  104.                     });
  105.  
  106.                     AlertDialog alert11 = builder1.create();
  107.                     alert11.show();
  108.                     return true;
  109.                 }
  110.             });
  111.         }
  112.     }
  113.  
  114.     private void openTextual(String profile)
  115.     {
  116.         SharedPreferences prefs= this.getSharedPreferences(profile, Context.MODE_PRIVATE);
  117.         user=prefs.getString("User", "").toString();
  118.         url=prefs.getString("URL", "");
  119.         password=prefs.getString("Password", "");
  120.         DB=prefs.getString("DB", "");
  121.         port = prefs.getInt("Port", 5432);
  122.         Log.d("openTextual","Profile Name: "+profile+"\n"+"URL: "+url+"\n"+"Username: "+user+"\n"+"Password: "+password+"\n"+"Database Name: "+DB+"\n"+"Port Name: "+port);
  123.         Intent intent=new Intent(this,TextQuery.class);
  124.         intent.putExtra("User",user);
  125.         intent.putExtra("URL", url);
  126.         intent.putExtra("DB",DB);
  127.         intent.putExtra("Password",password);
  128.         intent.putExtra("Port",port);
  129.         startActivity(intent);
  130.     }
  131.  
  132.     private void openGraphical(String profile)
  133.     {
  134.         SharedPreferences prefs= this.getSharedPreferences(profile, Context.MODE_PRIVATE);
  135.         user=prefs.getString("User", "").toString();
  136.         url=prefs.getString("URL", "");
  137.         password=prefs.getString("Password", "");
  138.         DB=prefs.getString("DB", "");
  139.         port = prefs.getInt("Port", 5432);
  140.         Log.d("openGraphical", "Profile Name: " + profile + "\n" + "URL: " + url + "\n" + "Username: " + user + "\n" + "Password: " + password + "\n" + "Database Name: " + DB + "\n" + "Port Name: " + port);
  141.         Intent intent=new Intent(this,Graphical_query.class);
  142.         intent.putExtra("User",user);
  143.         intent.putExtra("URL", url);
  144.         intent.putExtra("DB",DB);
  145.         intent.putExtra("Password",password);
  146.         intent.putExtra("Port",port);
  147.         startActivity(intent);
  148.     }
  149.  
  150.     @Override
  151.     protected void onResume()
  152.     {
  153.         super.onResume();
  154.         populateList();
  155.     }
  156.     @Override
  157.     public boolean onCreateOptionsMenu(Menu menu)
  158.     {
  159.         // Inflate the menu; this adds items to the action bar if it is present.
  160.         getMenuInflater().inflate(R.menu.menu_main, menu);
  161.         return true;
  162.     }
  163.  
  164.     @Override
  165.     public boolean onOptionsItemSelected(MenuItem item)
  166.     {
  167.         // Handle action bar item clicks here. The action bar will
  168.         // automatically handle clicks on the Home/Up button, so long
  169.         // as you specify a parent activity in AndroidManifest.xml.
  170.         int id = item.getItemId();
  171.  
  172.         //noinspection SimplifiableIfStatement
  173.         switch (id)
  174.         {
  175.             case R.id.NewConn:
  176.             {
  177.                 Intent intent =new Intent(this,NewConnection.class);
  178.                 startActivity(intent);
  179.             }
  180.             break;
  181.             case R.id.action_settings:
  182.             {
  183.                 Intent intent =new Intent(this,SettingsActivity.class);
  184.                 startActivity(intent);
  185.             }
  186.             break;
  187.         }
  188.         return super.onOptionsItemSelected(item);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement