Skip to content

Commit

Permalink
remove history elements by long-clicking
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlythe committed Sep 18, 2012
1 parent 5c3b500 commit 6444fe4
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 112 deletions.
82 changes: 41 additions & 41 deletions res/layout-port/main.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,52 +54,52 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="11.5" > android:layout_weight="11.5" >
<com.android2.calculator3.CalculatorViewPager <com.android2.calculator3.CalculatorViewPager
android:id="@+id/panelswitch" android:id="@+id/panelswitch"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="@dimen/barHeight" /> android:layout_marginTop="@dimen/barHeight" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:baselineAligned="false" android:baselineAligned="false"
android:orientation="horizontal" > android:orientation="horizontal" >
<RelativeLayout <RelativeLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="3" > android:layout_weight="3" >
<com.android2.calculator3.Slider <com.android2.calculator3.Slider
android:id="@+id/pulldown" android:id="@+id/pulldown"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background" android:background="@color/background"
android:scrollbarThumbHorizontal="@drawable/btn_function" > android:scrollbarThumbHorizontal="@drawable/btn_function" >
<include layout="@layout/history_panel" /> <include layout="@layout/history_panel" />
</com.android2.calculator3.Slider> </com.android2.calculator3.Slider>
</RelativeLayout> </RelativeLayout>
<FrameLayout <FrameLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="@dimen/barHeight"> android:layout_height="@dimen/barHeight">
<!-- marginRight has to be 0 to catch border-touch --> <!-- marginRight has to be 0 to catch border-touch -->
<com.android2.calculator3.ColorButton <com.android2.calculator3.ColorButton
android:id="@+id/clear" android:id="@+id/clear"
android:text="@string/clear" android:text="@string/clear"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginRight="0dp" android:layout_marginRight="0dp"
android:textSize="15dp" android:textSize="15dp"
style="@style/button_style" /> style="@style/button_style" />
<!-- marginRight has to be 0 to catch border-touch --> <!-- marginRight has to be 0 to catch border-touch -->
<com.android2.calculator3.ColorButton <com.android2.calculator3.ColorButton
android:id="@+id/del" android:id="@+id/del"
android:text="@string/del" android:text="@string/del"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginRight="0dp" android:layout_marginRight="0dp"
android:textSize="15dp" android:textSize="15dp"
style="@style/button_style" /> style="@style/button_style" />
</FrameLayout> </FrameLayout>
</LinearLayout> </LinearLayout>
</RelativeLayout> </RelativeLayout>
</LinearLayout> </LinearLayout>
8 changes: 5 additions & 3 deletions res/values/strings.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<string name="cos">cos</string> <string name="cos">cos</string>
<!-- Displayed on buttons on screen for the tan function. --> <!-- Displayed on buttons on screen for the tan function. -->
<string name="tan">tan</string> <string name="tan">tan</string>

<!-- Do not translate. Unicode pi sign; don't translate. Displayed as button on screen. --> <!-- Do not translate. Unicode pi sign; don't translate. Displayed as button on screen. -->
<string name="pi" translatable="false">\u03c0</string> <string name="pi" translatable="false">\u03c0</string>
<!-- Displayed on buttons on screen for the e function. --> <!-- Displayed on buttons on screen for the e function. -->
Expand Down Expand Up @@ -114,6 +114,8 @@
<string name="clear_history">Clear history</string> <string name="clear_history">Clear history</string>
<!-- Toast shown when text is copied to the clipboard. --> <!-- Toast shown when text is copied to the clipboard. -->
<string name="text_copied_toast">Text copied.</string> <string name="text_copied_toast">Text copied.</string>
<!-- History menu item for removing item from history -->
<string name="remove_from_history">Remove from history</string>


<string name="i" translatable="false">i</string> <string name="i" translatable="false">i</string>
<string name="percentage" translatable="false">%</string> <string name="percentage" translatable="false">%</string>
Expand All @@ -139,7 +141,7 @@
<string name="hex">Hex</string> <string name="hex">Hex</string>
<string name="bin">Bin</string> <string name="bin">Bin</string>
<string name="dec">Dec</string> <string name="dec">Dec</string>

<string name="hexDesc">Hexadecimal</string> <string name="hexDesc">Hexadecimal</string>
<string name="binDesc">Binary</string> <string name="binDesc">Binary</string>
<string name="decDesc">Decimal</string> <string name="decDesc">Decimal</string>
Expand All @@ -149,5 +151,5 @@
<string name="settings">Settings</string> <string name="settings">Settings</string>
<string name="showHistory">Show History</string> <string name="showHistory">Show History</string>
<string name="hideHistory">Hide History</string> <string name="hideHistory">Hide History</string>

<string name="returnToBasic">Return to the basic panel</string></resources> <string name="returnToBasic">Return to the basic panel</string></resources>
Expand Down
6 changes: 6 additions & 0 deletions src/com/android2/calculator3/Calculator.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -477,11 +477,17 @@ private void setUpHistory() {
base.setMaxWidth(mPulldown.getWidth()/2); base.setMaxWidth(mPulldown.getWidth()/2);
base.setText(he.getBase()); base.setText(he.getBase());
base.setHistoryEntry(he); base.setHistoryEntry(he);
base.setHistory(mHistory);
base.setRowView(entry);
base.setHistoryView(mHistoryView);
HistoryTextView edited = (HistoryTextView) entry.findViewById(R.id.edited); HistoryTextView edited = (HistoryTextView) entry.findViewById(R.id.edited);
edited.setOnLongClickListener(this); edited.setOnLongClickListener(this);
edited.setText(he.getEdited()); edited.setText(he.getEdited());
edited.setMaxWidth(mPulldown.getWidth()-base.getWidth()-entry.getChildAt(1).getWidth()); edited.setMaxWidth(mPulldown.getWidth()-base.getWidth()-entry.getChildAt(1).getWidth());
edited.setHistoryEntry(he); edited.setHistoryEntry(he);
edited.setHistory(mHistory);
edited.setRowView(entry);
edited.setHistoryView(mHistoryView);
mHistoryView.addView(entry); mHistoryView.addView(entry);
} }
} }
Expand Down
16 changes: 8 additions & 8 deletions src/com/android2/calculator3/CalculatorDisplay.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,63 +107,63 @@ public boolean onKeyDown(View view, Editable content, int keyCode,
String textAfterInsertionHandle = getText().toString().substring(selectionHandle, getText().length()); String textAfterInsertionHandle = getText().toString().substring(selectionHandle, getText().length());


if(textBeforeInsertionHandle.endsWith(mSinString + "(")) { if(textBeforeInsertionHandle.endsWith(mSinString + "(")) {
int deletionLength = mSinString.length()+1; int deletionLength = mSinString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mCosString + "(")) { else if(textBeforeInsertionHandle.endsWith(mCosString + "(")) {
int deletionLength = mCosString.length()+1; int deletionLength = mCosString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mTanString + "(")) { else if(textBeforeInsertionHandle.endsWith(mTanString + "(")) {
int deletionLength = mTanString.length()+1; int deletionLength = mTanString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mLogString + "(")) { else if(textBeforeInsertionHandle.endsWith(mLogString + "(")) {
int deletionLength = mLogString.length()+1; int deletionLength = mLogString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mModString + "(")) { else if(textBeforeInsertionHandle.endsWith(mModString + "(")) {
int deletionLength = mModString.length()+1; int deletionLength = mModString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mLnString + "(")) { else if(textBeforeInsertionHandle.endsWith(mLnString + "(")) {
int deletionLength = mLnString.length()+1; int deletionLength = mLnString.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mDX + "(")) { else if(textBeforeInsertionHandle.endsWith(mDX + "(")) {
int deletionLength = mDX.length()+1; int deletionLength = mDX.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
setSelection(selectionHandle-deletionLength); setSelection(selectionHandle-deletionLength);
return true; return true;
} }
else if(textBeforeInsertionHandle.endsWith(mDY + "(")) { else if(textBeforeInsertionHandle.endsWith(mDY + "(")) {
int deletionLength = mDY.length()+1; int deletionLength = mDY.length()+1;
String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) + String text = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length()-deletionLength) +
textAfterInsertionHandle; textAfterInsertionHandle;
setText(text); setText(text);
Expand Down
4 changes: 2 additions & 2 deletions src/com/android2/calculator3/EventListener.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void run() {
case R.id.parentheses: case R.id.parentheses:
if(mHandler.getText().equals(mErrorString)) mHandler.setText(""); if(mHandler.getText().equals(mErrorString)) mHandler.setText("");
if(mHandler.getText().contains("=")) { if(mHandler.getText().contains("=")) {
String[] equation = mHandler.getText().split("="); String[] equation = mHandler.getText().split("=");
mHandler.setText(equation[0] + "=(" + equation[1] + ")"); mHandler.setText(equation[0] + "=(" + equation[1] + ")");
} }
else{ else{
Expand All @@ -176,7 +176,7 @@ public void run() {
case R.id.mod: case R.id.mod:
if(mHandler.getText().equals(mErrorString)) mHandler.setText(""); if(mHandler.getText().equals(mErrorString)) mHandler.setText("");
if(mHandler.getText().contains("=")) { if(mHandler.getText().contains("=")) {
String[] equation = mHandler.getText().split("="); String[] equation = mHandler.getText().split("=");
if(equation.length>1) { if(equation.length>1) {
mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ","); mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ",");
} }
Expand Down
5 changes: 5 additions & 0 deletions src/com/android2/calculator3/History.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ String getText() {
String getBase() { String getBase() {
return current().getBase(); return current().getBase();
} }

void remove(HistoryEntry he) {
mEntries.remove(he);
mPos--;
}
} }
53 changes: 46 additions & 7 deletions src/com/android2/calculator3/HistoryTextView.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;


public class HistoryTextView extends TextView { public class HistoryTextView extends TextView {
private static final int COPY = 0; private static final int COPY = 0;
private static final int REMOVE = 1;
private String[] mMenuItemsStrings; private String[] mMenuItemsStrings;
private HistoryEntry mHistoryEntry; private HistoryEntry mHistoryEntry;
private History mHistory;
private LinearLayout mRowView;
private LinearLayout mHistoryView;


public HistoryTextView(Context context, AttributeSet attrs) { public HistoryTextView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
Expand All @@ -32,8 +37,9 @@ public void onCreateContextMenu(ContextMenu menu) {
MenuHandler handler = new MenuHandler(); MenuHandler handler = new MenuHandler();
if (mMenuItemsStrings == null) { if (mMenuItemsStrings == null) {
Resources resources = getResources(); Resources resources = getResources();
mMenuItemsStrings = new String[1]; mMenuItemsStrings = new String[2];
mMenuItemsStrings[COPY] = resources.getString(android.R.string.copy) + " \"" + getText().toString() + "\""; mMenuItemsStrings[COPY] = resources.getString(android.R.string.copy) + " \"" + getText().toString() + "\"";
mMenuItemsStrings[REMOVE] = resources.getString(R.string.remove_from_history);
} }
for (int i = 0; i < mMenuItemsStrings.length; i++) { for (int i = 0; i < mMenuItemsStrings.length; i++) {
menu.add(Menu.NONE, i, i, mMenuItemsStrings[i]).setOnMenuItemClickListener(handler); menu.add(Menu.NONE, i, i, mMenuItemsStrings[i]).setOnMenuItemClickListener(handler);
Expand All @@ -52,6 +58,10 @@ public boolean onTextContextMenuItem(CharSequence title) {
copyContent(); copyContent();
handled = true; handled = true;
} }
else if (TextUtils.equals(title, mMenuItemsStrings[REMOVE])) {
removeContent();
handled = true;
}
return handled; return handled;
} }


Expand All @@ -62,11 +72,40 @@ private void copyContent() {
Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
} }


public HistoryEntry getHistoryEntry() { private void removeContent() {
return mHistoryEntry; mHistory.remove(mHistoryEntry);
} mHistoryView.removeView(mRowView);
}

public HistoryEntry getHistoryEntry() {
return mHistoryEntry;
}

public void setHistoryEntry(HistoryEntry historyEntry) {
this.mHistoryEntry = historyEntry;
}

public History getHistory() {
return mHistory;
}

public void setHistory(History history) {
this.mHistory = history;
}


public void setHistoryEntry(HistoryEntry historyEntry) { public LinearLayout getRowView() {
this.mHistoryEntry = historyEntry; return mRowView;
} }

public void setRowView(LinearLayout rowView) {
this.mRowView = rowView;
}

public LinearLayout getHistoryView() {
return mHistoryView;
}

public void setHistoryView(LinearLayout historyView) {
this.mHistoryView = historyView;
}
} }
Loading

0 comments on commit 6444fe4

Please sign in to comment.