Skip to content

Commit

Permalink
MainActivity: create dialog link to Shaali home page
Browse files Browse the repository at this point in the history
  • Loading branch information
dimtion committed Apr 2, 2018
1 parent 2e17209 commit 15e0670
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 52 deletions.
164 changes: 114 additions & 50 deletions app/src/main/java/com/dimtion/shaarlier/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -14,12 +15,15 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;


public class MainActivity extends AppCompatActivity {
private boolean m_isNoAccount;
Expand Down Expand Up @@ -144,66 +148,126 @@ public boolean onCreateOptionsMenu(Menu menu) {

@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
switch (id) {
case R.id.action_share:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
shareDialog();
break;
case R.id.action_open_shaarli:
openShaarliDialog();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}

private void shareDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(getString(R.string.share));

// TODO: move this to a xml file:
final LinearLayout layout = new LinearLayout(this);

final TextView textView = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
//noinspection deprecation
textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
} else {
textView.setTextAppearance(android.R.style.TextAppearance_Medium);
}
textView.setText(getText(R.string.text_new_url));

// Set an EditText view to get user input
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
input.setHint(getText(R.string.hint_new_url));

layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(10, 10, 20, 20);

layout.addView(textView);
layout.addView(input);
alert.setView(layout);

alert.setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Intent intent = new Intent(getBaseContext(), AddActivity.class);
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, value);
startActivity(intent);
}
});

alert.setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();
}

alert.setTitle(getString(R.string.share));
private void openShaarliDialog() {
AccountsSource accountsSource = new AccountsSource(this);
accountsSource.rOpen();
final List<ShaarliAccount> accounts = accountsSource.getAllAccounts();
accountsSource.close();

// TODO : move this to a xml file :
final LinearLayout layout = new LinearLayout(this);
if (accounts == null || accounts.size() < 1) {
return;
}
if (accounts.size() == 1) {
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(accounts.get(0).getUrlShaarli())
);
startActivity(browserIntent);
}

final TextView textView = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
//noinspection deprecation
textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
} else {
textView.setTextAppearance(android.R.style.TextAppearance_Medium);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(getString(R.string.action_open_shaarli));

// TODO: move this to a xml file:
final LinearLayout layout = new LinearLayout(this);
for (final ShaarliAccount account : accounts) {
Button b = new Button(this);
b.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
b.setText(account.getShortName());
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(account.getUrlShaarli())
);
startActivity(browserIntent);
}
textView.setText(getText(R.string.text_new_url));

// Set an EditText view to get user input
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
input.setHint(getText(R.string.hint_new_url));

layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(10, 10, 20, 20);

layout.addView(textView);
layout.addView(input);
alert.setView(layout);

alert.setPositiveButton(getString(android.R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Intent intent = new Intent(getBaseContext(), AddActivity.class);
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, value);
startActivity(intent);
}
});

alert.setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();
break;
default:
return true;
});

layout.addView(b);
}

return super.onOptionsItemSelected(item);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(10, 10, 20, 20);

alert.setView(layout);

alert.setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();
}

private boolean isHandlingHttpScheme() {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
android:icon="@drawable/ic_action_new"
android:orderInCategory="99"
android:title="@string/action_share"
app:showAsAction="always" />
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_open_shaarli"
android:icon="@drawable/ic_action_new"
android:orderInCategory="99"
android:title="@string/action_open_shaarli"
app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ Cette application est fièrement développée par dimtion, vous pouvez me trouve
<string name="advanced">Avancé</string>
<string name="tweet">Tweeter</string>
<string name="use_twitter">Twitter (nécessite shaarli2twitter)</string>
<string name="action_open_shaarli">Ouvrir Shaarli</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<string name="action_share">Share a link</string>
<string name="text_version">Version unknown</string>
<string name="hint_new_url">(empty for a new note)</string>
<string name="text_new_url">Link to share :</string>
<string name="text_new_url">Link to share:</string>
<string name="text_url" translatable="false">Url :</string>
<string name="text_url_hint">(leave empty for a new note)</string>
<string name="title">Title :</string>
Expand Down Expand Up @@ -95,4 +95,5 @@
<string name="tweet">Tweet</string>
<string name="use_twitter">Use twitter (needs shaarli2twitter plugin)</string>
<string name="advanced">Advanced</string>
<string name="action_open_shaarli">Open Shaarli</string>
</resources>

0 comments on commit 15e0670

Please sign in to comment.