package com.joinmarrow.marrow; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.joinmarrow.marrow.tasks.LogonTask; import org.jdeferred.Deferred; import org.jdeferred.DoneCallback; import org.jdeferred.FailCallback; import org.jdeferred.Promise; import org.jdeferred.impl.DeferredObject; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import us.monoid.json.JSONArray; import us.monoid.json.JSONException; import us.monoid.json.JSONObject; import us.monoid.web.Resty; public class MarrowActivity extends Activity { private TextView userNameText; private ListView linkList; private String username; private String password; private String marrowSite; public SharedPreferences sharedPref; private class MarrowItem { public String iTitle; public String iUrl; public MarrowItem(String title, String url) { iTitle = title; iUrl = url; } public String toString() { return iTitle; } } List<MarrowItem> linkData = new ArrayList<MarrowItem>(); private class GetSubscriptions extends AsyncTask<String, Void, JSONObject> { protected JSONObject doInBackground(String... user) { JSONObject result = new JSONObject(); if (null != user && password != "") { try { result = new Resty() .json(marrowSite.concat("/api/bones/subscriptions")) .toObject(); } catch (IOException | JSONException e) { Log.e("marrow", e.getMessage()); } finally { Log.i("marrow", result.toString()); } } return result; } protected void onPostExecute(JSONObject result) { ListView linkList = (ListView) findViewById(R.id.linkList); JSONArray data = new JSONArray(); try { data = result.getJSONArray("marrow"); } catch (JSONException e) { Log.i("marrow", "unexpected JSONException"); e.printStackTrace(); Log.d("marrow", result.toString()); } linkData.clear(); for (int x = 0; x < data.length(); x++) { try { linkData.add( new MarrowItem( data.getJSONObject(x).getString("title"), data.getJSONObject(x).getString("url") ) ); } catch (JSONException e) { e.printStackTrace(); } } ((ArrayAdapter<String>) linkList.getAdapter()).notifyDataSetChanged(); } } private class GetBoneTask extends AsyncTask<String, Void, JSONObject> { protected JSONObject doInBackground(String... user) { JSONObject result = new JSONObject(); if (null != user) { try { result = new Resty() .json(marrowSite.concat("/api/bones/u/").concat(user[0])) .toObject(); } catch (IOException | JSONException e) { Log.e("marrow", e.getMessage()); } finally { Log.i("marrow", result.toString()); } } return result; } protected void onPostExecute(JSONObject result) { ListView linkList = (ListView) findViewById(R.id.linkList); JSONArray data = new JSONArray(); try { data = result.getJSONArray("marrow"); } catch (JSONException e) { Log.i("marrow", "unexpected JSONException"); e.printStackTrace(); Log.d("marrow", result.toString()); } linkData.clear(); for (int x = 0; x < data.length(); x++) { try { linkData.add( new MarrowItem( data.getJSONObject(x).getString("title"), data.getJSONObject(x).getString("url") ) ); } catch (JSONException e) { e.printStackTrace(); } } ((ArrayAdapter<String>) linkList.getAdapter()).notifyDataSetChanged(); } } public void goButtonClicked(View view) { String userName = userNameText.getText().toString().trim(); new GetBoneTask().execute(userName); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(userNameText.getWindowToken(), 0); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_marrow); userNameText = (TextView) findViewById(R.id.userName); userNameText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean result = false; if (v.getText() != "") { goButtonClicked(v); result = true; } return result; } }); linkList = (ListView) findViewById(R.id.linkList); linkList.setAdapter( new ArrayAdapter<MarrowItem>( this, android.R.layout.simple_list_item_1, linkData ) ); linkList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MarrowItem item = linkData.get(position); Log.i("Marrow", item.iUrl); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.iUrl)); startActivity(browserIntent); } }); final MarrowActivity self = this; sharedPref = PreferenceManager.getDefaultSharedPreferences(this); username = sharedPref.getString("username", "test"); password = sharedPref.getString("password", ""); marrowSite = sharedPref.getString("site", "https://joinmarrow.com"); if (! password.equals("")) { Deferred<Boolean, Void, Void> waitForLogon = new DeferredObject(); Promise<Boolean, Void, Void> logonPromise = waitForLogon.promise(); new LogonTask(waitForLogon).execute(username, password, marrowSite); logonPromise.done(new DoneCallback<Boolean>() { @Override public void onDone(Boolean success) { if (success) { new GetSubscriptions().execute(); } else { startActivity(new Intent(self, LoginActivity.class)); } } }).fail(new FailCallback<Void>() { @Override public void onFail(Void result) { new GetBoneTask().execute(username); } }); } SharedPreferences.OnSharedPreferenceChangeListener spl = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.i("marrow", key); if (key.equals("username")) { username = sharedPreferences.getString("username", ""); } else if (key.equals("password")) { Log.i("marrow", "changing Password"); password = sharedPreferences.getString("password", ""); Deferred<Boolean, Void, Void> waitForLogon = new DeferredObject<Boolean, Void, Void>(); Promise<Boolean, Void, Void> logonPromise = waitForLogon.promise(); new LogonTask(waitForLogon).execute(); logonPromise.done(new DoneCallback<Boolean>() { @Override public void onDone(Boolean result) { if (result) { new GetSubscriptions().execute(); } else { startActivity(new Intent(self, LoginActivity.class)); } } }); } else if (key.equals("marrowSite")) { marrowSite = sharedPreferences.getString("site", "https://joinmarrow.com"); } } }; sharedPref.registerOnSharedPreferenceChangeListener(spl); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_marrow, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } }