#21 Check for status.php

This commit is contained in:
Stefan Niedermann 2015-10-23 10:04:03 +02:00
parent 97ad2a2fc6
commit 8c23698546
4 changed files with 48 additions and 39 deletions

View file

@ -65,6 +65,10 @@ public class SettingsActivity extends AppCompatActivity {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String url = ((EditText) findViewById(R.id.settings_url)).getText().toString();
if (!url.endsWith("/")) {
url += "/";
}
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
@ -130,7 +134,7 @@ public class SettingsActivity extends AppCompatActivity {
@Override
protected Boolean doInBackground(String... params) {
return NotesClientUtil.isValidURL(params[0]);
return NotesClientUtil.pingServer(params[0]);
}
@Override

View file

@ -24,18 +24,17 @@ public class AllNotesWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
for (int appWidgetId : appWidgetIds) {
// Set up the intent that starts the StackViewService, which will
// provide the views for this collection.
Intent intent = new Intent(context, StackWidgetService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Instantiate the RemoteViews object for the app widget layout.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_all_notes);
rv.setRemoteAdapter(appWidgetIds[i], intent);
rv.setRemoteAdapter(appWidgetId, intent);
appWidgetManager.updateAppWidget(appWidgetId, rv);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.layout.widget_all_notes);
@ -50,8 +49,7 @@ public class AllNotesWidget extends AppWidgetProvider {
}
class StackRemoteViewsFactory implements
RemoteViewsService.RemoteViewsFactory {
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final int mCount = 10;
private List<Note> mWidgetItems = new ArrayList<>();
private Context mContext;

View file

@ -22,18 +22,15 @@ import it.niedermann.owncloud.notes.model.Note;
public class NotesClient {
public static final String METHOD_GET = "GET";
public static final String METHOD_PUT = "PUT";
public static final String METHOD_POST = "POST";
public static final String METHOD_DELETE = "DELETE";
private static final String key_id = "id";
private static final String key_title = "title";
private static final String key_content = "content";
private static final String key_modified = "modified";
private static final String application_json = "application/json";
private static final String method_get = "GET";
private static final String method_put = "PUT";
private static final String method_post = "POST";
private static final String method_delete = "DELETE";
private String url = "";
private String username = "";
private String password = "";
@ -47,7 +44,7 @@ public class NotesClient {
public List<Note> getNotes() throws JSONException,
IOException {
List<Note> notesList = new ArrayList<>();
JSONArray notes = new JSONArray(requestServer("notes", method_get, null));
JSONArray notes = new JSONArray(requestServer("notes", METHOD_GET, null));
long noteId = 0;
String noteTitle = "";
String noteContent = "";
@ -92,7 +89,7 @@ public class NotesClient {
String noteContent = "";
Calendar noteModified = null;
JSONObject currentItem = new JSONObject(
requestServer("notes/" + id, method_get, null));
requestServer("notes/" + id, METHOD_GET, null));
if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong(key_id);
@ -127,7 +124,7 @@ public class NotesClient {
JSONObject paramObject = new JSONObject();
paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer("notes", method_post,
JSONObject currentItem = new JSONObject(requestServer("notes", METHOD_POST,
paramObject));
if (!currentItem.isNull(key_id)) {
@ -155,7 +152,7 @@ public class NotesClient {
JSONObject paramObject = new JSONObject();
paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer(
"notes/" + noteId, method_put, paramObject));
"notes/" + noteId, METHOD_PUT, paramObject));
if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString(key_title);
@ -170,7 +167,7 @@ public class NotesClient {
public void deleteNote(long noteId) throws
IOException {
this.requestServer("notes/" + noteId, method_delete, null);
this.requestServer("notes/" + noteId, METHOD_DELETE, null);
}
/**

View file

@ -2,7 +2,12 @@ package it.niedermann.owncloud.notes.util;
import android.util.Base64;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@ -23,26 +28,6 @@ public class NotesClientUtil {
return url != null && url.length() > 4 && url.startsWith("http") && url.charAt(4) != 's';
}
/**
* Checks if the given URL returns a valid status code and sets the Check next to the URL-Input Field to visible.
* @param urlStr String URL
* @return URL is valid
*/
public static boolean isValidURL(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 10); // mTimeout is in seconds
urlc.connect();
return urlc.getResponseCode() == 200;
} catch (MalformedURLException el) {
return false;
} catch (IOException e) {
return false;
}
}
/**
*
* @param url String
@ -74,4 +59,29 @@ public class NotesClientUtil {
return false;
}
/**
* Pings a server and checks if there is a installed ownCloud instance
*
* @param url String URL to server
* @return true if there is a installed instance, false if not
*/
public static boolean pingServer(String url) {
StringBuffer result = new StringBuffer();
try {
HttpURLConnection con = (HttpURLConnection) new URL(url + "status.php")
.openConnection();
con.setRequestMethod(NotesClient.METHOD_GET);
con.setConnectTimeout(10 * 1000); // 10 seconds
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject response = new JSONObject(result.toString());
return response.getBoolean("installed");
} catch (IOException | JSONException e) {
return false;
}
}
}