OpenRatActivity.java (6307B)
1 /* 2 * Openrat CMS-Client for Android 3 * 4 * Copyright (C) 2011 Jan Dankert 5 * 6 * This program is free software: you can redistribute it and/or modify it under 7 * the terms of the GNU General Public License as published by the Free Software 8 * Foundation, either version 3 of the License, or (at your option) any later 9 * version. 10 * 11 * This program is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 * details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 package de.openrat.android.client; 20 21 import java.io.IOException; 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.List; 25 26 import android.app.AlertDialog; 27 import android.app.ListActivity; 28 import android.content.Intent; 29 import android.content.SharedPreferences; 30 import android.os.Bundle; 31 import android.preference.PreferenceManager; 32 import android.text.TextUtils; 33 import android.util.Log; 34 import android.view.Menu; 35 import android.view.MenuInflater; 36 import android.view.MenuItem; 37 import android.view.View; 38 import android.widget.AdapterView; 39 import android.widget.ImageView; 40 import android.widget.ListView; 41 import android.widget.TextView; 42 import android.widget.AdapterView.OnItemClickListener; 43 import android.widget.AdapterView.OnItemLongClickListener; 44 import de.openrat.android.client.adapter.SimpleNameAdapter; 45 import de.openrat.android.client.util.OpenRatClientAsyncTask; 46 import de.openrat.android.client.util.ServerList; 47 import de.openrat.client.OpenRatClient; 48 49 /** 50 * @author Jan Dankert 51 */ 52 public class OpenRatActivity extends ListActivity 53 { 54 private static final String PREFS_NAME = "OR_BLOG_PREFS"; 55 private OpenRatClient client; 56 private List<String> serverList; 57 58 /** Called when the activity is first created. */ 59 @Override 60 public void onCreate(Bundle savedInstanceState) 61 { 62 super.onCreate(savedInstanceState); 63 setContentView(R.layout.listing); 64 65 ImageView image = (ImageView) findViewById(R.id.listimage); 66 image.setImageResource(R.drawable.openrat); 67 image.setVisibility(View.VISIBLE); 68 69 TextView title = (TextView) findViewById(R.id.listtitle); 70 title.setText(getResources().getString(R.string.connect)); 71 title.setVisibility(View.VISIBLE); 72 73 SharedPreferences globalPrefs = PreferenceManager 74 .getDefaultSharedPreferences(this); 75 serverList = Arrays.asList(TextUtils.split(globalPrefs.getString( 76 "server", ""), ",")); 77 78 ArrayList<String> list = new ArrayList<String>(); 79 for (String server : serverList) 80 { 81 SharedPreferences preferences = getSharedPreferences(server, 82 MODE_PRIVATE); 83 84 list.add(preferences.getString("name", "?")); 85 } 86 87 if (list.size() == 0) 88 { 89 // Noch kein Server konfiguriert. Hinweis anzeigen! 90 final AlertDialog.Builder builder = new AlertDialog.Builder(this); 91 builder.setMessage(getResources().getString(R.string.noserver)); 92 AlertDialog alert = builder.create(); 93 alert.show(); 94 } 95 96 final SimpleNameAdapter adapter = new SimpleNameAdapter(this, list, 97 android.R.drawable.ic_menu_set_as); 98 99 ListView lv = getListView(); 100 lv.setAdapter(adapter); 101 102 lv.setOnItemClickListener(new OnItemClickListener() 103 { 104 105 @Override 106 public void onItemClick(AdapterView<?> arg0, View arg1, 107 final int pos, long rowId) 108 { 109 new OpenRatClientAsyncTask(OpenRatActivity.this, 110 R.string.waitingforlogin) 111 { 112 @Override 113 protected void callServer() throws IOException 114 { 115 SharedPreferences prefs = getSharedPreferences( 116 serverList.get(pos), MODE_PRIVATE); 117 118 int port = Integer.parseInt(prefs.getString("port", 119 "80")); 120 String path = prefs.getString("path", "/"); 121 String host = prefs.getString("hostname", ""); 122 String dbid = prefs.getString("database", ""); 123 124 client = new OpenRatClient(host, path, port); 125 126 final String username = prefs.getString("username", ""); 127 client.login(username, prefs.getString("password", ""), 128 dbid); 129 Log.d(OpenRatActivity.this.getClass().getSimpleName(), 130 "User login: " + username); 131 132 } 133 134 protected void doOnSuccess() 135 { 136 // Verbindung und Login waren erfolgreich. 137 // Jetzt zur Projekt-Liste wechseln. 138 final Intent intent = new Intent(OpenRatActivity.this, 139 ProjectActivity.class); 140 intent.putExtra(ProjectActivity.CLIENT, client); 141 startActivity(intent); 142 }; 143 144 }.execute(); 145 146 } 147 148 }); 149 150 lv.setOnItemLongClickListener(new OnItemLongClickListener() 151 { 152 @Override 153 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 154 final int pos, long rowId) 155 { 156 Intent intent = new Intent(OpenRatActivity.this, Server.class); 157 intent.putExtra(Server.NAME, serverList.get(pos)); 158 startActivity(intent); 159 return true; 160 } 161 }); 162 163 } 164 165 @Override 166 public boolean onCreateOptionsMenu(Menu menu) 167 { 168 super.onCreateOptionsMenu(menu); 169 MenuInflater mi = new MenuInflater(getApplication()); 170 mi.inflate(R.menu.main, menu); 171 172 return true; 173 } 174 175 public boolean onOptionsItemSelected(MenuItem item) 176 { 177 switch (item.getItemId()) 178 { 179 case R.id.menu_preferences: 180 startActivity(new Intent(this, Configuration.class)); 181 return true; 182 case R.id.menu_newserver: 183 184 SharedPreferences globalPrefs = PreferenceManager 185 .getDefaultSharedPreferences(this); 186 String newServername = "" + System.currentTimeMillis(); 187 188 ServerList list = new ServerList(globalPrefs.getString( 189 "server", "")).addServer(newServername); 190 globalPrefs.edit().putString("server", list.toPlain()).commit(); 191 192 Intent intent = new Intent(this, Server.class); 193 intent.putExtra(Server.NAME, newServername); 194 startActivity(intent); 195 return true; 196 } 197 return false; 198 } 199 200 @Override 201 protected void onStop() 202 { 203 super.onStop(); 204 205 // Save user preferences. We need an Editor object to 206 // make changes. All objects are from android.context.Context 207 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 208 SharedPreferences.Editor editor = settings.edit(); 209 // editor.putBoolean("silentMode", mSilentMode); 210 211 // Don't forget to commit your edits!!! 212 editor.commit(); 213 } 214 }