diff --git a/app/build.gradle b/app/build.gradle index d585040..9f614eb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,9 +20,12 @@ android { targetCompatibility 1.8 sourceCompatibility 1.8 } - dataBinding { + dataBinding { enabled = true -} + } + dexOptions { + javaMaxHeapSize "2048M" + } } dependencies { @@ -38,12 +41,11 @@ dependencies { implementation "android.arch.lifecycle:extensions:1.1.0" annotationProcessor "android.arch.lifecycle:compiler:1.1.0" - // Java8 support for Lifecycles implementation "android.arch.lifecycle:common-java8:1.1.0" - compile 'org.jsoup:jsoup:1.10.1' - compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2' + implementation 'org.jsoup:jsoup:1.10.1' + implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c052e2f..e9fdff5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -42,12 +42,12 @@ - - + + diff --git a/app/src/main/java/eu/stuifzand/micropub/GetPageTask.java b/app/src/main/java/eu/stuifzand/micropub/GetPageTask.java deleted file mode 100644 index 373fa3a..0000000 --- a/app/src/main/java/eu/stuifzand/micropub/GetPageTask.java +++ /dev/null @@ -1,50 +0,0 @@ -package eu.stuifzand.micropub; - -import android.os.AsyncTask; -import android.view.View; -import android.widget.Toast; - -import java.io.IOException; - -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -public class GetPageTask extends AsyncTask { - protected View view; - public GetPageTask(View view) - { - this.view = view; - } - - @Override - protected String doInBackground(String... strings) { - Request request = new Request.Builder() - .url(strings[0]) - .build(); - - OkHttpClient client = new OkHttpClient(); - String msg; - Call call = client.newCall(request); - Response response = null; - try { - response = call.execute(); - msg = Integer.toString(response.code()); - return msg; - } - catch (IOException e) { - return e.getMessage(); - } - finally { - if (response != null) { - response.close(); - } - } - } - - protected void onPostExecute(String message) { - String url = "https://peterstuifzand.nl/"; - Toast.makeText(this.view.getContext(), message, 1).show(); - } -} diff --git a/app/src/main/java/eu/stuifzand/micropub/ListAdapter.java b/app/src/main/java/eu/stuifzand/micropub/ListAdapter.java new file mode 100644 index 0000000..20876ea --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/ListAdapter.java @@ -0,0 +1,58 @@ +package eu.stuifzand.micropub; + +import android.content.Context; +import android.databinding.BindingAdapter; +import android.databinding.DataBindingUtil; +import android.databinding.ObservableArrayList; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; + +import eu.stuifzand.micropub.client.Syndication; +import eu.stuifzand.micropub.databinding.ListItemBinding; + +public class ListAdapter extends BaseAdapter { + private ObservableArrayList list; + private LayoutInflater inflater; + + public ListAdapter(ObservableArrayList l) { + list = l; + } + + @Override + public int getCount() { + return list.size(); + } + + @Override + public Object getItem(int position) { + return list.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (inflater == null) { + inflater = (LayoutInflater) parent.getContext() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + ListItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.list_item, parent, false); + binding.setInfo(list.get(position)); + + return binding.getRoot(); + } + + @BindingAdapter("app:list") + public static void bindList(ListView view, ObservableArrayList list) { + assert list != null; + ListAdapter adapter = new ListAdapter(list); + view.setAdapter(adapter); + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java index 9d0f6bb..57dda53 100644 --- a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java @@ -6,26 +6,27 @@ import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; -import android.app.Activity; +import android.app.Application; import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; -import android.widget.Toast; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import eu.stuifzand.micropub.databinding.ActivityMainBinding; -import eu.stuifzand.micropub.eu.stuifzand.micropub.client.Client; -import eu.stuifzand.micropub.eu.stuifzand.micropub.client.Post; +import eu.stuifzand.micropub.client.Client; +import eu.stuifzand.micropub.client.Post; +import eu.stuifzand.micropub.client.Syndication; import okhttp3.HttpUrl; public class MainActivity extends AppCompatActivity { @@ -36,7 +37,9 @@ public class MainActivity extends AppCompatActivity { // setContentView(R.layout.activity_main); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class); + Client client = ViewModelProviders.of(MainActivity.this).get(Client.class); binding.setViewModel(model); + binding.setClient(client); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -63,6 +66,15 @@ public class MainActivity extends AppCompatActivity { // } // }); + TokenReady callback = (accountType, accountName, token) -> { + Log.i("micropub", "TokenReady called " + accountType + " " + accountName + " " + token); + client.setToken(accountType, accountName, token); + client.loadSyndicates(); + }; + + AccountManager am = AccountManager.get(this); + Bundle options = new Bundle(); + am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(callback), null); } @Override @@ -84,8 +96,6 @@ public class MainActivity extends AppCompatActivity { return true; } if (id == R.id.action_send) { -// EditText mEdit = (EditText)findViewById(R.id.editContent); -// new PostMessageTask(this.findViewById(R.id.editContent)).execute("http://192.168.178.21:5000/micropub", mEdit.getText().toString()); } return super.onOptionsItemSelected(item); @@ -94,14 +104,51 @@ public class MainActivity extends AppCompatActivity { public void sendPost(View view) { AccountManager am = AccountManager.get(this); Bundle options = new Bundle(); - am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(true), null); + TokenReady callback = new TokenReady() { + @Override + public void tokenReady(String accountType, String accountName, String token) { + + PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class); + AccountManager am = AccountManager.get(MainActivity.this); + Account[] accounts = am.getAccountsByType(accountType); + + String micropubBackend = null; + for (Account account : accounts) { + if (account.name.equals(accountName)) { + micropubBackend = am.getUserData(account, "micropub"); + } + } + + if (micropubBackend != null) { + Log.i("micropub", "Sending message to " + micropubBackend); + Client client = ViewModelProviders.of(MainActivity.this).get(Client.class); + client.getResponse().observe(MainActivity.this, response -> { + Log.i("micropub", "response received " + response.isSuccess()); + if (response.isSuccess()) { + model.clear(); + } + }); + Post post = new Post(null, model.content.get(), model.category.get(), HttpUrl.parse(model.inReplyTo.get())); + List uids = new ArrayList(); + for (Syndication s : client.syndicates) { + if (s.checked.get()) { + uids.add(s.uid.get()); + } + } + post.setSyndicationUids(uids.toArray(new String[uids.size()])); + client.createPost(post, token, HttpUrl.parse(micropubBackend)); + } + } + }; + + am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(callback), null); } - private class OnTokenAcquired implements AccountManagerCallback { - private boolean sendMessage; + public class OnTokenAcquired implements AccountManagerCallback { + private final TokenReady callback; - public OnTokenAcquired(boolean sendMessage) { - this.sendMessage = sendMessage; + public OnTokenAcquired(TokenReady callback) { + this.callback = callback; } @Override @@ -111,41 +158,15 @@ public class MainActivity extends AppCompatActivity { Bundle bundle = result.getResult(); Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT); if (launch != null) { - startActivityForResult(launch, 0); + MainActivity.this.startActivityForResult(launch, 0); return; } // The token is a named value in the bundle. The name of the value // is stored in the constant AccountManager.KEY_AUTHTOKEN. String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); - - if (sendMessage) { - PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class); - AccountManager am = AccountManager.get(MainActivity.this); - Account[] accounts = am.getAccountsByType(bundle.getString("accountType")); - String accountName = bundle.getString("authAccount"); - - String micropubBackend = null; - for (Account account : accounts) { - if (account.name.equals(accountName)) { - micropubBackend = am.getUserData(account, "micropub"); - } - } - - if (micropubBackend != null) { - Log.i("micropub", "Sending message to " + micropubBackend); - Client client = new Client(getApplication()); - client.getResponse().observe(MainActivity.this, response -> { - Log.i("micropub", "response received " + response.isSuccess()); - if (response.isSuccess()) { - model.clear(); - } - }); - client.createPost(new Post(null, model.content.get(), model.category.get(), HttpUrl.parse(model.inReplyTo.get())), token, HttpUrl.parse(micropubBackend)); - } - } - Log.d("micropub", "GetTokenForAccount Bundle is " + token); + callback.tokenReady(bundle.getString("accountType"), bundle.getString("authAccount"), token); } catch (OperationCanceledException e) { e.printStackTrace(); } catch (IOException e) { @@ -155,23 +176,4 @@ public class MainActivity extends AppCompatActivity { } } } - - public void startSignin(View view) { - AccountManager am = AccountManager.get(this); - Bundle options = new Bundle(); - am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(false), null); - } - - private void showMessage(final String msg) { - if (TextUtils.isEmpty(msg)) - return; - - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); - } - }); - } - } diff --git a/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java b/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java index 245f4d6..fc16328 100644 --- a/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java +++ b/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java @@ -57,7 +57,6 @@ public class PostMessageTask extends AsyncTask { .addInterceptor(logging) .build(); - String msg; Call call = client.newCall(request); Response response = null; diff --git a/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java b/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java index 14ee931..6cb6587 100644 --- a/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java +++ b/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java @@ -1,7 +1,14 @@ package eu.stuifzand.micropub; import android.arch.lifecycle.ViewModel; +import android.databinding.ObservableArrayList; import android.databinding.ObservableField; +import android.databinding.ObservableList; + +import java.util.Arrays; + +import eu.stuifzand.micropub.client.Client; +import eu.stuifzand.micropub.client.Syndication; public class PostViewModel extends ViewModel { public final ObservableField content = new ObservableField<>(); diff --git a/app/src/main/java/eu/stuifzand/micropub/TokenReady.java b/app/src/main/java/eu/stuifzand/micropub/TokenReady.java new file mode 100644 index 0000000..69b15e4 --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/TokenReady.java @@ -0,0 +1,5 @@ +package eu.stuifzand.micropub; + +public interface TokenReady { + void tokenReady(String type, String name, String token); +} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AccountsActivity.java b/app/src/main/java/eu/stuifzand/micropub/auth/AccountsActivity.java similarity index 85% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AccountsActivity.java rename to app/src/main/java/eu/stuifzand/micropub/auth/AccountsActivity.java index 45f8a42..a2afd57 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AccountsActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/AccountsActivity.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatedActivity.java similarity index 96% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java rename to app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatedActivity.java index 3b26247..d4f96b3 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatedActivity.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.content.Intent; import android.os.Bundle; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticationActivity.java similarity index 97% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java rename to app/src/main/java/eu/stuifzand/micropub/auth/AuthenticationActivity.java index ebd1205..d7d714c 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticationActivity.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.accounts.Account; import android.accounts.AccountAuthenticatorActivity; @@ -8,7 +8,6 @@ import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; -import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java b/app/src/main/java/eu/stuifzand/micropub/auth/Authenticator.java similarity index 98% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java rename to app/src/main/java/eu/stuifzand/micropub/auth/Authenticator.java index 70a63ec..b758b03 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/Authenticator.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.accounts.AbstractAccountAuthenticator; import android.accounts.Account; @@ -13,8 +13,6 @@ import android.text.TextUtils; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.w3c.dom.Text; - import java.io.IOException; import okhttp3.Call; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatorService.java b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatorService.java similarity index 92% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatorService.java rename to app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatorService.java index 13dd82b..6b1949d 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatorService.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/AuthenticatorService.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.app.Service; import android.content.Intent; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java b/app/src/main/java/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java similarity index 97% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java rename to app/src/main/java/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java index cbad8a6..30adc57 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/VerifyAuthenticationTask.java @@ -1,8 +1,7 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.accounts.AccountAuthenticatorResponse; import android.accounts.AccountManager; -import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebSigninActivity.java b/app/src/main/java/eu/stuifzand/micropub/auth/WebSigninActivity.java similarity index 94% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebSigninActivity.java rename to app/src/main/java/eu/stuifzand/micropub/auth/WebSigninActivity.java index a732729..b3d9ae5 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebSigninActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/WebSigninActivity.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.accounts.AccountAuthenticatorResponse; import android.accounts.AccountManager; diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java b/app/src/main/java/eu/stuifzand/micropub/auth/WebsigninTask.java similarity index 97% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java rename to app/src/main/java/eu/stuifzand/micropub/auth/WebsigninTask.java index c5e68b4..918530a 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java +++ b/app/src/main/java/eu/stuifzand/micropub/auth/WebsigninTask.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.auth; +package eu.stuifzand.micropub.auth; import android.accounts.AccountAuthenticatorResponse; import android.accounts.AccountManager; diff --git a/app/src/main/java/eu/stuifzand/micropub/client/Client.java b/app/src/main/java/eu/stuifzand/micropub/client/Client.java new file mode 100644 index 0000000..9d97228 --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/client/Client.java @@ -0,0 +1,199 @@ +package eu.stuifzand.micropub.client; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.MutableLiveData; +import android.databinding.ObservableArrayList; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.NonNull; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import eu.stuifzand.micropub.MainActivity; +import eu.stuifzand.micropub.TokenReady; +import eu.stuifzand.micropub.auth.VerifyAuthenticationTask; +import okhttp3.Call; +import okhttp3.FormBody; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.logging.HttpLoggingInterceptor; + +public class Client extends AndroidViewModel { + private MutableLiveData response = new MutableLiveData<>(); + public final ObservableArrayList syndicates = new ObservableArrayList<>(); + + private String accountType; + private String accountName; + private String token; + + public Client(@NonNull Application application) { + super(application); + } + + class LoadSyndicatesTask extends AsyncTask { + + @Override + protected String doInBackground(String... strings) { + AccountManager am = AccountManager.get(Client.this.getApplication()); + Account[] accounts = am.getAccountsByType(accountType); + String micropubBackend = null; + for (Account account : accounts) { + if (account.name.equals(accountName)) { + micropubBackend = am.getUserData(account, "micropub"); + break; + } + } + if (micropubBackend != null) { + HttpUrl backend = HttpUrl.parse(micropubBackend); + backend = backend.newBuilder() + .setQueryParameter("q", "syndicate-to") + .build(); + + Request request = new Request.Builder() + .addHeader("Authorization", "Bearer " + token) + .method("GET", null) + .url(backend) + .build(); + + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + logging.setLevel(HttpLoggingInterceptor.Level.BODY); + + OkHttpClient client = new OkHttpClient.Builder() + .addInterceptor(logging) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); + + Call call = client.newCall(request); + okhttp3.Response httpResponse = null; + try { + httpResponse = call.execute(); + if (httpResponse.code() == 200) { + JsonParser parser = new JsonParser(); + JsonObject element = parser.parse(httpResponse.body().string()).getAsJsonObject(); + JsonArray arr = element.getAsJsonArray("syndicate-to"); + syndicates.clear(); + for (int i = 0; i < arr.size(); i++) { + JsonObject syn = arr.get(i).getAsJsonObject(); + syndicates.add(new Syndication(syn.get("uid").getAsString(), syn.get("name").getAsString())); + } + } + } catch (IOException e) { + } finally { + if (httpResponse != null) { + httpResponse.close(); + } + } + } + return null; + } + } + + public void loadSyndicates() { + new LoadSyndicatesTask().execute(); + } + + public void createPost(Post post, String accessToken, HttpUrl micropubBackend) { + new PostTask(post, micropubBackend, accessToken, response).execute(); + } + + public LiveData getResponse() { + return response; + } + + public void setToken(String accountType, String accountName, String token) { + this.accountType = accountType; + this.accountName = accountName; + this.token = token; + } + + private class PostTask extends AsyncTask { + + private Post post; + private HttpUrl micropubBackend; + private String accessToken; + private MutableLiveData response; + + PostTask(Post post, HttpUrl micropubBackend, String accessToken, MutableLiveData response) { + this.post = post; + this.micropubBackend = micropubBackend; + this.accessToken = accessToken; + this.response = response; + } + + @Override + protected Void doInBackground(String... strings) { + FormBody.Builder builder = new FormBody.Builder(); + builder.add("h", "entry") + .add("content", post.getContent()); + + for (String cat : post.getCategories()) { + builder.add("category[]", cat); + } + + for (String uid : post.getSyndicationUids()) { + builder.add("mp-syndicate-to[]", uid); + } + + if (post.hasInReplyTo()) { + builder.add("in-reply-to", post.getInReplyTo()); + } + + if (post.hasName()) { + builder.add("name", post.getName()); + } + + RequestBody formBody = builder.build(); + + Request request = new Request.Builder() + .addHeader("Authorization", "Bearer " + accessToken) + .method("POST", formBody) + .url(micropubBackend) + .build(); + + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + logging.setLevel(HttpLoggingInterceptor.Level.BODY); + + OkHttpClient client = new OkHttpClient.Builder() + .addInterceptor(logging) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); + + Call call = client.newCall(request); + okhttp3.Response httpResponse = null; + try { + httpResponse = call.execute(); + if (httpResponse.code() == 201) { + String location = httpResponse.header("Location"); + response.postValue(Response.successful(location)); + } else { + response.postValue(Response.failed()); + } + for (Syndication s : Client.this.syndicates) { + s.checked.set(false); + } + } catch (IOException e) { + response.postValue(Response.failed()); + } finally { + if (httpResponse != null) { + httpResponse.close(); + } + } + return null; + } + } +} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java b/app/src/main/java/eu/stuifzand/micropub/client/Post.java similarity index 72% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java rename to app/src/main/java/eu/stuifzand/micropub/client/Post.java index cd7f89a..b3700bd 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java +++ b/app/src/main/java/eu/stuifzand/micropub/client/Post.java @@ -1,36 +1,40 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.client; +package eu.stuifzand.micropub.client; import okhttp3.HttpUrl; public class Post { private String name; private String content; - private String[] categories; private HttpUrl inReplyTo; + private String[] categories; + private String[] syndicationUids; public Post(String content) { this.content = content; this.categories = new String[]{}; + this.syndicationUids = new String[]{}; } public Post(String name, String content) { this.name = name; this.content = content; this.categories = new String[]{}; + this.syndicationUids = new String[]{}; } public Post(String name, String content, String categories) { this.name = name; this.content = content; this.categories = categories.split("\\s+"); + this.syndicationUids = new String[]{}; } public Post(String name, String content, String categories, HttpUrl inReplyTo) { this(name, content, categories); this.inReplyTo = inReplyTo; + this.syndicationUids = new String[]{}; } - public String getContent() { return content; } @@ -38,6 +42,7 @@ public class Post { public boolean hasName() { return this.name != null && !name.equals(""); } + public String getName() { return this.name; } @@ -53,4 +58,12 @@ public class Post { public String[] getCategories() { return categories; } + + public void setSyndicationUids(String[] syndicationUids) { + this.syndicationUids = syndicationUids; + } + + public String[] getSyndicationUids() { + return syndicationUids; + } } diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java b/app/src/main/java/eu/stuifzand/micropub/client/Response.java similarity index 89% rename from app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java rename to app/src/main/java/eu/stuifzand/micropub/client/Response.java index 90c6ec4..18aa3eb 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java +++ b/app/src/main/java/eu/stuifzand/micropub/client/Response.java @@ -1,4 +1,4 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.client; +package eu.stuifzand.micropub.client; public class Response { private boolean success; diff --git a/app/src/main/java/eu/stuifzand/micropub/client/Syndication.java b/app/src/main/java/eu/stuifzand/micropub/client/Syndication.java new file mode 100644 index 0000000..a4bc5cb --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/client/Syndication.java @@ -0,0 +1,18 @@ +package eu.stuifzand.micropub.client; + +import android.databinding.Observable; +import android.databinding.ObservableBoolean; +import android.databinding.ObservableField; +import android.util.Log; + +public class Syndication { + public ObservableBoolean checked = new ObservableBoolean(); + public ObservableField uid = new ObservableField<>(); + public ObservableField name = new ObservableField<>(); + + public Syndication(String uid, String name) { + this.checked.set(false); + this.uid.set(uid); + this.name.set(name); + } +} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java deleted file mode 100644 index 78d48d3..0000000 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java +++ /dev/null @@ -1,106 +0,0 @@ -package eu.stuifzand.micropub.eu.stuifzand.micropub.client; - -import android.app.Application; -import android.arch.lifecycle.AndroidViewModel; -import android.arch.lifecycle.LiveData; -import android.arch.lifecycle.MutableLiveData; -import android.os.AsyncTask; -import android.support.annotation.NonNull; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.FormBody; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.logging.HttpLoggingInterceptor; - -public class Client extends AndroidViewModel { - private MutableLiveData response = new MutableLiveData<>(); - - public Client(@NonNull Application application) { - super(application); - } - - public void createPost(Post post, String accessToken, HttpUrl micropubBackend) { - new PostTask(post, micropubBackend, accessToken, response).execute(); - } - - public LiveData getResponse() { - return response; - } - - private static class PostTask extends AsyncTask { - - private Post post; - private HttpUrl micropubBackend; - private String accessToken; - private MutableLiveData response; - - PostTask(Post post, HttpUrl micropubBackend, String accessToken, MutableLiveData response) { - this.post = post; - this.micropubBackend = micropubBackend; - this.accessToken = accessToken; - this.response = response; - } - - @Override - protected Void doInBackground(String... strings) { - FormBody.Builder builder = new FormBody.Builder(); - builder.add("h", "entry") - .add("content", post.getContent()); - - for (String cat : post.getCategories()) { - builder.add("category[]", cat); - } - - if (post.hasInReplyTo()) { - builder.add("in-reply-to", post.getInReplyTo()); - } - - if (post.hasName()) { - builder.add("name", post.getName()); - } - - RequestBody formBody = builder.build(); - micropubBackend = HttpUrl.parse("http://192.168.178.21:5000/micropub"); - Request request = new Request.Builder() - .addHeader("Authorization", "Bearer " + accessToken) - .method("POST", formBody) - .url(micropubBackend) - .build(); - - HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); - logging.setLevel(HttpLoggingInterceptor.Level.BODY); - - OkHttpClient client = new OkHttpClient.Builder() - .addInterceptor(logging) - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(30, TimeUnit.SECONDS) - .build(); - - Call call = client.newCall(request); - okhttp3.Response httpResponse = null; - try { - httpResponse = call.execute(); - if (httpResponse.code() == 201) { - String location = httpResponse.header("Location"); - response.postValue(Response.successful(location)); - } else { - response.postValue(Response.failed()); - } - } catch (IOException e) { - response.postValue(Response.failed()); - } finally { - if (httpResponse != null) { - httpResponse.close(); - } - } - return null; - } - } -} diff --git a/app/src/main/res/layout/activity_authenticated.xml b/app/src/main/res/layout/activity_authenticated.xml index f5b1b0f..25c515c 100644 --- a/app/src/main/res/layout/activity_authenticated.xml +++ b/app/src/main/res/layout/activity_authenticated.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="eu.stuifzand.micropub.eu.stuifzand.micropub.auth.AuthenticatedActivity"> + tools:context="eu.stuifzand.micropub.auth.AuthenticatedActivity"> + tools:context="eu.stuifzand.micropub.auth.WebSigninActivity"> + + + app:client="@{client}" + tools:layout_editor_absoluteY="-4dp" + android:layout_width="match_parent" /> + + + tools:context="eu.stuifzand.micropub.MainActivity" + tools:showIn="@layout/activity_main"> + + + app:layout_constraintTop_toBottomOf="@id/labelCounter" /> - + android:layout_marginStart="8dp" + android:ems="10" + android:gravity="top" + android:hint="Content" + android:imeOptions="actionNext" + android:inputType="textMultiLine" + android:nextFocusForward="@id/editCategory" + android:padding="3dp" + android:singleLine="false" + android:text="@={viewModel.content}" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/editInReplyTo" + tools:layout_editor_absoluteY="101dp" /> + app:layout_constraintTop_toBottomOf="@id/content" /> + + + + + +