| 1 | package net.mandaria.tippytipper.activities; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import com.flurry.android.FlurryAgent; |
| 7 | |
| 8 | import net.mandaria.tippytipper.R; |
| 9 | import net.mandaria.tippytipper.TippyTipperApplication; |
| 10 | import android.app.Activity; |
| 11 | import android.content.Intent; |
| 12 | import android.os.Bundle; |
| 13 | import android.view.*; |
| 14 | import android.view.View.OnClickListener; |
| 15 | import android.widget.*; |
| 16 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 17 | |
| 18 | public class Total extends Activity { |
| 19 | |
| 20 | /** Called when the activity is first created. */ |
| 21 | @Override |
| 22 | public void onCreate(Bundle savedInstanceState) { |
| 23 | super.onCreate(savedInstanceState); |
| 24 | setContentView(R.layout.total); |
| 25 | |
| 26 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 27 | |
| 28 | calculateTip(appState.service.getTipPercentageAsDouble()); |
| 29 | |
| 30 | View btn_SplitBill = findViewById(R.id.btn_SplitBill); |
| 31 | btn_SplitBill.setOnClickListener(new OnClickListener() |
| 32 | { |
| 33 | public void onClick(View v) |
| 34 | { |
| 35 | splitBillWithDefaultNumberOfPeople(); |
| 36 | |
| 37 | Intent i = new Intent(getBaseContext(), SplitBill.class); |
| 38 | startActivity(i); |
| 39 | } |
| 40 | }); |
| 41 | // Drawable d_SplitBill = findViewById(R.id.btn_SplitBill).getBackground(); |
| 42 | // int green = Color.parseColor("#216C2A"); |
| 43 | // PorterDuffColorFilter filter_green = new PorterDuffColorFilter(green, PorterDuff.Mode.SRC_ATOP); |
| 44 | // d_SplitBill.setColorFilter(filter_green); |
| 45 | |
| 46 | View btn_round_down = findViewById(R.id.btn_round_down); |
| 47 | btn_round_down.setOnClickListener(new OnClickListener() |
| 48 | { |
| 49 | public void onClick(View v) |
| 50 | { |
| 51 | roundDown(); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | View btn_round_up = findViewById(R.id.btn_round_up); |
| 56 | btn_round_up.setOnClickListener(new OnClickListener() |
| 57 | { |
| 58 | public void onClick(View v) |
| 59 | { |
| 60 | roundUp(); |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | SeekBar seek_tip_percentage = (SeekBar)findViewById(R.id.seek_tip_percentage); |
| 65 | seek_tip_percentage.setOnSeekBarChangeListener(new OnSeekBarChangeListener() |
| 66 | { |
| 67 | @Override |
| 68 | public void onProgressChanged(SeekBar seekBar, int progress, |
| 69 | boolean fromUser) |
| 70 | { |
| 71 | if(fromUser) |
| 72 | { |
| 73 | System.out.println("Log mio: onProgressChanged, fromUser"); |
| 74 | calculateTip((double)progress); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onStartTrackingTouch(SeekBar seekBar) |
| 80 | { |
| 81 | // TODO Auto-generated method stub |
| 82 | |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onStopTrackingTouch(SeekBar seekBar) |
| 87 | { |
| 88 | System.out.println("Log mio: onStopTrackingTouch"); |
| 89 | tipPercentageSliderOnStopTouch(); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | /* Called after onCreate or onRestart */ |
| 96 | @Override |
| 97 | public void onStart() |
| 98 | { |
| 99 | super.onStart(); |
| 100 | boolean enableErrorLogging = Settings.getEnableErrorLogging(getBaseContext()); |
| 101 | String API = getString(R.string.flurrykey); |
| 102 | if(!API.equals("") && enableErrorLogging == true) |
| 103 | { |
| 104 | FlurryAgent.setContinueSessionMillis(30000); |
| 105 | FlurryAgent.onStartSession(this, API); |
| 106 | } |
| 107 | |
| 108 | refreshBillAmount(); |
| 109 | |
| 110 | Button btn_TipAmount1 = (Button)findViewById(R.id.btn_TipAmount1); |
| 111 | double tipPercentagePresetOne = Settings.getTipPercentagePresetOne(getBaseContext()); |
| 112 | btn_TipAmount1.setText((int)tipPercentagePresetOne + "%"); |
| 113 | btn_TipAmount1.setOnClickListener(new OnClickListener() |
| 114 | { |
| 115 | public void onClick(View v) |
| 116 | { |
| 117 | double tipPercentagePresetOne = Settings.getTipPercentagePresetOne(getBaseContext()); |
| 118 | calculateTip(tipPercentagePresetOne); |
| 119 | |
| 120 | Map<String, String> params = new HashMap<String, String>(); |
| 121 | params.put("Tip Percentage Preset", String.valueOf(tipPercentagePresetOne)); |
| 122 | FlurryAgent.onEvent("Tip Preset One Button", params); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | Button btn_TipAmount2 = (Button)findViewById(R.id.btn_TipAmount2); |
| 127 | double tipPercentagePresetTwo = Settings.getTipPercentagePresetTwo(getBaseContext()); |
| 128 | btn_TipAmount2.setText((int)tipPercentagePresetTwo + "%"); |
| 129 | btn_TipAmount2.setOnClickListener(new OnClickListener() |
| 130 | { |
| 131 | public void onClick(View v) |
| 132 | { |
| 133 | double tipPercentagePresetTwo = Settings.getTipPercentagePresetTwo(getBaseContext()); |
| 134 | calculateTip(tipPercentagePresetTwo); |
| 135 | |
| 136 | Map<String, String> params = new HashMap<String, String>(); |
| 137 | params.put("Tip Percentage Preset", String.valueOf(tipPercentagePresetTwo)); |
| 138 | FlurryAgent.onEvent("Tip Preset Two Button", params); |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | Button btn_TipAmount3 = (Button)findViewById(R.id.btn_TipAmount3); |
| 143 | double tipPercentagePresetThree = Settings.getTipPercentagePresetThree(getBaseContext()); |
| 144 | btn_TipAmount3.setText((int)tipPercentagePresetThree + "%"); |
| 145 | btn_TipAmount3.setOnClickListener(new OnClickListener() |
| 146 | { |
| 147 | public void onClick(View v) |
| 148 | { |
| 149 | double tipPercentagePresetThree = Settings.getTipPercentagePresetThree(getBaseContext()); |
| 150 | calculateTip(tipPercentagePresetThree); |
| 151 | |
| 152 | Map<String, String> params = new HashMap<String, String>(); |
| 153 | params.put("Tip Percentage Preset", String.valueOf(tipPercentagePresetThree)); |
| 154 | FlurryAgent.onEvent("Tip Preset Three Button", params); |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onStop() |
| 161 | { |
| 162 | super.onStop(); |
| 163 | FlurryAgent.onEndSession(this); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean onCreateOptionsMenu(Menu menu) |
| 168 | { |
| 169 | super.onCreateOptionsMenu(menu); |
| 170 | MenuInflater inflater = getMenuInflater(); |
| 171 | inflater.inflate(R.menu.menu, menu); |
| 172 | FlurryAgent.onEvent("Menu Button"); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean onOptionsItemSelected(MenuItem item) |
| 178 | { |
| 179 | switch(item.getItemId()) |
| 180 | { |
| 181 | case R.id.settings: |
| 182 | startActivity(new Intent(this, Settings.class)); |
| 183 | FlurryAgent.onEvent("Settings Button"); |
| 184 | return true; |
| 185 | case R.id.about: |
| 186 | startActivity(new Intent(this, About.class)); |
| 187 | FlurryAgent.onEvent("About Button"); |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | private void tipPercentageSliderOnStopTouch() |
| 194 | { |
| 195 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 196 | Map<String, String> params = new HashMap<String, String>(); |
| 197 | params.put("Bill Amount", appState.service.getBillAmount()); |
| 198 | params.put("Tax Amount", appState.service.getTaxAmount()); |
| 199 | params.put("Tip Amount", appState.service.getTipAmount()); |
| 200 | params.put("Tip Percentage", appState.service.getTipPercentage()); |
| 201 | params.put("Tax Percentage", appState.service.getTaxPercentage()); |
| 202 | |
| 203 | FlurryAgent.onEvent("Tip Percentage Slider", params); |
| 204 | } |
| 205 | |
| 206 | private void splitBillWithDefaultNumberOfPeople() |
| 207 | { |
| 208 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 209 | int numberOfPeople = Settings.getDefaultNumberOfPeopleToSplitBill(getBaseContext()); |
| 210 | |
| 211 | Map<String, String> params = new HashMap<String, String>(); |
| 212 | params.put("Default Number Of People", String.valueOf(numberOfPeople)); |
| 213 | params.put("Bill Amount", appState.service.getBillAmount()); |
| 214 | params.put("Tax Amount", appState.service.getTaxAmount()); |
| 215 | params.put("Tip Amount", appState.service.getTipAmount()); |
| 216 | params.put("Total Amount", appState.service.getTotalAmount()); |
| 217 | params.put("Tip Percentage", appState.service.getTipPercentage()); |
| 218 | params.put("Tax Percentage", appState.service.getTaxPercentage()); |
| 219 | FlurryAgent.onEvent("Split Bill Button", params); |
| 220 | |
| 221 | appState.service.splitBill(numberOfPeople); |
| 222 | } |
| 223 | |
| 224 | private void calculateTip(Double percent) |
| 225 | { |
| 226 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 227 | float excludeTaxRate = Settings.getExcludeTaxRate(getBaseContext()); |
| 228 | boolean enableExcludeTaxRate = Settings.getEnableExcludeTaxRate(getBaseContext()); |
| 229 | if(enableExcludeTaxRate == false) |
| 230 | excludeTaxRate = 0; |
| 231 | |
| 232 | //appState.service.CalculateTip(percent/100.0); |
| 233 | appState.service.calculateTip(percent/100.0, excludeTaxRate/100.0); |
| 234 | |
| 235 | bindData(); |
| 236 | } |
| 237 | |
| 238 | private void roundDown() |
| 239 | { |
| 240 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 241 | boolean roundTip = Settings.isSetToRoundByTip(getBaseContext()); |
| 242 | |
| 243 | String preRoundTipAmount = appState.service.getTipAmount(); |
| 244 | String preRoundTotalAmount = appState.service.getTotalAmount(); |
| 245 | |
| 246 | appState.service.roundDown(roundTip); |
| 247 | |
| 248 | Map<String, String> params = new HashMap<String, String>(); |
| 249 | params.put("Round Tip", String.valueOf(roundTip)); |
| 250 | params.put("Pre Round Tip Amount", preRoundTipAmount); |
| 251 | params.put("Pre Round Total Amount", preRoundTotalAmount); |
| 252 | params.put("Bill Amount", appState.service.getBillAmount()); |
| 253 | params.put("Tax Amount", appState.service.getTaxAmount()); |
| 254 | params.put("Tip Amount", appState.service.getTipAmount()); |
| 255 | params.put("Total Amount", appState.service.getTotalAmount()); |
| 256 | FlurryAgent.onEvent("Round Down Button", params); |
| 257 | |
| 258 | bindData(); |
| 259 | } |
| 260 | |
| 261 | private void roundUp() |
| 262 | { |
| 263 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 264 | boolean roundTip = Settings.isSetToRoundByTip(getBaseContext()); |
| 265 | |
| 266 | String preRoundTipAmount = appState.service.getTipAmount(); |
| 267 | String preRoundTotalAmount = appState.service.getTotalAmount(); |
| 268 | |
| 269 | appState.service.roundUp(roundTip); |
| 270 | |
| 271 | Map<String, String> params = new HashMap<String, String>(); |
| 272 | params.put("Round Tip", String.valueOf(roundTip)); |
| 273 | params.put("Pre Round Tip Amount", preRoundTipAmount); |
| 274 | params.put("Pre Round Total Amount", preRoundTotalAmount); |
| 275 | params.put("Bill Amount", appState.service.getBillAmount()); |
| 276 | params.put("Tax Amount", appState.service.getTaxAmount()); |
| 277 | params.put("Tip Amount", appState.service.getTipAmount()); |
| 278 | params.put("Total Amount", appState.service.getTotalAmount()); |
| 279 | FlurryAgent.onEvent("Round Up Button", params); |
| 280 | |
| 281 | bindData(); |
| 282 | } |
| 283 | |
| 284 | public void refreshBillAmount() |
| 285 | { |
| 286 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 287 | double tipPercent = appState.service.getTipPercentageAsDouble(); |
| 288 | float excludeTaxRate = Settings.getExcludeTaxRate(getBaseContext()); |
| 289 | boolean enableExcludeTaxRate = Settings.getEnableExcludeTaxRate(getBaseContext()); |
| 290 | if(enableExcludeTaxRate == false) |
| 291 | excludeTaxRate = 0; |
| 292 | |
| 293 | if(excludeTaxRate == 0) |
| 294 | { |
| 295 | appState.service.refreshBillAmount(); |
| 296 | appState.service.calculateTip(appState.service.getTipPercentageAsDouble()/100.0, excludeTaxRate); |
| 297 | bindData(); |
| 298 | } |
| 299 | else |
| 300 | calculateTip(tipPercent); |
| 301 | } |
| 302 | |
| 303 | private void bindData() |
| 304 | { |
| 305 | TippyTipperApplication appState = ((TippyTipperApplication)this.getApplication()); |
| 306 | |
| 307 | TextView lbl_bill_amount = (TextView)findViewById(R.id.lbl_bill_amount); |
| 308 | TextView lbl_tip_amount = (TextView)findViewById(R.id.lbl_tip_amount); |
| 309 | TextView lbl_total_amount = (TextView)findViewById(R.id.lbl_total_amount); |
| 310 | //TextView lbl_tip_percentage = (TextView)findViewById(R.id.lbl_tip_percentage); |
| 311 | SeekBar seek_tip_percentage = (SeekBar)findViewById(R.id.seek_tip_percentage); |
| 312 | View inflated_excludetax = findViewById(R.id.inflated_excludeTax); |
| 313 | TextView lbl_tip_text = (TextView)findViewById(R.id.lbl_tip_text); |
| 314 | |
| 315 | float excludeTaxRate = Settings.getExcludeTaxRate(getBaseContext()); |
| 316 | boolean enableExcludeTaxRate = Settings.getEnableExcludeTaxRate(getBaseContext()); |
| 317 | if(enableExcludeTaxRate == false) |
| 318 | excludeTaxRate = 0; |
| 319 | |
| 320 | if(excludeTaxRate != 0) |
| 321 | { |
| 322 | ViewStub stub_excludeTax = (ViewStub)findViewById(R.id.stub_excludeTax); |
| 323 | if(stub_excludeTax != null) |
| 324 | stub_excludeTax.setVisibility(View.VISIBLE); |
| 325 | else if(inflated_excludetax != null) |
| 326 | inflated_excludetax.setVisibility(View.VISIBLE); |
| 327 | TextView lbl_tax_amount = (TextView)findViewById(R.id.lbl_tax_amount); |
| 328 | lbl_tax_amount.setText(appState.service.getTaxAmount()); |
| 329 | |
| 330 | // Add (tax%): to label |
| 331 | TextView lbl_tax_text = (TextView)findViewById(R.id.lbl_tax_text); |
| 332 | String tax_text = lbl_tax_text.getText().toString(); |
| 333 | if(!tax_text.contains(appState.service.getTaxPercentage())) |
| 334 | lbl_tax_text.setText(this.getString(R.string.tax).replace(":", " (" + appState.service.getTaxPercentage() + "):")); |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | if(inflated_excludetax != null) |
| 339 | inflated_excludetax.setVisibility(View.GONE); |
| 340 | } |
| 341 | |
| 342 | // Add (tip%): to label |
| 343 | String tip_text = lbl_tip_text.getText().toString(); |
| 344 | if(!tip_text.contains(appState.service.getTipPercentage())) |
| 345 | lbl_tip_text.setText(this.getString(R.string.tip).replace(":", " (" + appState.service.getTipPercentage() + "):")); |
| 346 | |
| 347 | //lbl_tip_percentage.setText(appState.service.GetTipPercentage()); |
| 348 | lbl_bill_amount.setText(appState.service.getBillAmount()); |
| 349 | lbl_tip_amount.setText(appState.service.getTipAmount()); |
| 350 | lbl_total_amount.setText(appState.service.getTotalAmount()); |
| 351 | seek_tip_percentage.setProgress(appState.service.getTipPercentageRounded()); |
| 352 | } |
| 353 | } |