Cleaned up FormattingController

It was coded pretty badly, so I cleaned it up. Most notably, it used
a static instance of the Activity, which is a huge no-no which
creates memory leaks. The irony is, it already had a reference to the
Activity used correctly in the constructor. One memory-leak fixed,
29 more to go (LibreOfficeMainActivity holds that static Activity
object which needs fixing). Also, simplified the "bottom toolbar"
in preparation for the CoordinatorLayout implementation which will
allow the activity to have fancy animations and smart interactions.

Change-Id: I31aa117f6179910db73a5256b0a287357e1dec83
Reviewed-on: https://gerrit.libreoffice.org/33010
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
This commit is contained in:
aleksandar-stefanovic 2017-01-12 17:51:58 +01:00 committed by jan iversen
parent 15c97b28aa
commit 9123ba9632
3 changed files with 61 additions and 100 deletions

View File

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:id="@+id/toolbar_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:elevation="3dp"
android:background="@color/toolbar_background"
@ -14,119 +15,114 @@
app:theme="@style/LibreOfficeTheme.Toolbar"
tools:showIn="@layout/activity_main">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/formatting_toolbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button_bold"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_bold"/>
<ImageButton
android:id="@+id/button_italic"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_italic"/>
<ImageButton
android:id="@+id/button_underlined"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_underlined"/>
<ImageButton
android:id="@+id/button_strikethrough"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_strikethrough"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button_align_left"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_align_left"/>
<ImageButton
android:id="@+id/button_align_center"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_align_center"/>
<ImageButton
android:id="@+id/button_align_right"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_align_right"/>
<ImageButton
android:id="@+id/button_align_justify"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:background="@drawable/image_button_background"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:src="@drawable/ic_format_align_justify"/>
</LinearLayout>
<Spinner
android:id="@+id/font_name_spinner"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="50dip"/>
<Spinner
android:id="@+id/font_size_spinner"
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="50dip"/>
@ -136,25 +132,25 @@
<LinearLayout
android:id="@+id/search_toolbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/search_string"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"/>
<ImageButton
android:id="@+id/button_search_up"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@drawable/image_button_background"
android:padding="10dp"
@ -162,14 +158,13 @@
<ImageButton
android:id="@+id/button_search_down"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@drawable/image_button_background"
android:padding="10dp"
android:src="@drawable/ic_search_direction_up"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>

View File

@ -1,40 +1,35 @@
package org.libreoffice;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import org.libreoffice.kit.Document;
public class FormattingController implements View.OnClickListener {
class FormattingController implements View.OnClickListener {
private static final String LOGTAG = ToolbarController.class.getSimpleName();
private final Toolbar mToolbarBottom;
private LibreOfficeMainActivity mContext;
public FormattingController(LibreOfficeMainActivity context, Toolbar toolbarBottom) {
mToolbarBottom = toolbarBottom;
FormattingController(LibreOfficeMainActivity context) {
mContext = context;
((ImageButton) context.findViewById(R.id.button_bold)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_italic)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_strikethrough)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_underlined)).setOnClickListener(this);
mContext.findViewById(R.id.button_bold).setOnClickListener(this);
mContext.findViewById(R.id.button_italic).setOnClickListener(this);
mContext.findViewById(R.id.button_strikethrough).setOnClickListener(this);
mContext.findViewById(R.id.button_underlined).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_align_left)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_align_center)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_align_right)).setOnClickListener(this);
((ImageButton) context.findViewById(R.id.button_align_justify)).setOnClickListener(this);
mContext.findViewById(R.id.button_align_left).setOnClickListener(this);
mContext.findViewById(R.id.button_align_center).setOnClickListener(this);
mContext.findViewById(R.id.button_align_right).setOnClickListener(this);
mContext.findViewById(R.id.button_align_justify).setOnClickListener(this);
}
@Override
public void onClick(View view) {
ImageButton button = (ImageButton) view;
boolean selected = button.isSelected();
button.setSelected(selected);
if (selected) {
if (button.isSelected()) {
button.getBackground().setState(new int[]{-android.R.attr.state_selected});
} else {
button.getBackground().setState(new int[]{android.R.attr.state_selected});
@ -65,15 +60,13 @@ public class FormattingController implements View.OnClickListener {
case R.id.button_align_justify:
LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:JustifyPara"));
break;
default:
break;
}
}
public void onToggleStateChanged(final int type, final boolean selected) {
void onToggleStateChanged(final int type, final boolean selected) {
LOKitShell.getMainHandler().post(new Runnable() {
public void run() {
Integer buttonId = null;
Integer buttonId;
switch (type) {
case Document.BOLD:
buttonId = R.id.button_bold;
@ -104,8 +97,7 @@ public class FormattingController implements View.OnClickListener {
return;
}
LibreOfficeMainActivity activity = LibreOfficeMainActivity.mAppContext;
ImageButton button = (ImageButton) activity.findViewById(buttonId);
ImageButton button = (ImageButton) mContext.findViewById(buttonId);
button.setSelected(selected);
if (selected) {
button.getBackground().setState(new int[]{android.R.attr.state_selected});
@ -114,29 +106,5 @@ public class FormattingController implements View.OnClickListener {
}
}
});
/*if (menuItem == null) {
Log.e(LOGTAG, "MenuItem not found.");
return;
}
final Drawable drawable;
if (pressed) {
Resources resources = mContext.getResources();
Drawable[] layers = new Drawable[2];
layers[0] = resources.getDrawable(R.drawable.icon_background);
layers[1] = resources.getDrawable(drawableId);
drawable = new LayerDrawable(layers);
} else {
drawable = mContext.getResources().getDrawable(drawableId);
}
final MenuItem fMenuItem = menuItem;
LOKitShell.getMainHandler().post(new Runnable() {
public void run() {
fMenuItem.setIcon(drawable);
}
});*/
}
}

View File

@ -121,12 +121,11 @@ public class LibreOfficeMainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
hideBottomToolbar();
mToolbarController = new ToolbarController(this, getSupportActionBar(), toolbarTop);
mFormattingController = new FormattingController(this, toolbarBottom);
mFormattingController = new FormattingController(this);
toolbarTop.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -458,8 +457,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity {
LOKitShell.getMainHandler().post(new Runnable() {
@Override
public void run() {
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
if (toolbarBottom.getVisibility() != View.VISIBLE) {
if (findViewById(R.id.toolbar_bottom).getVisibility() != View.VISIBLE) {
showSoftKeyboardDirect();
}
}