PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/CSipSimple/src/com/csipsimple/wizards/impl/Ippi.java

https://bitbucket.org/bohlooli/csipsimple2
Java | 241 lines | 159 code | 45 blank | 37 comment | 15 complexity | 6be176919a1017ae9b4558e614cc78d5 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
  3. * This file is part of CSipSimple.
  4. *
  5. * CSipSimple is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. * If you own a pjsip commercial license you can also redistribute it
  10. * and/or modify it under the terms of the GNU Lesser General Public License
  11. * as an android library.
  12. *
  13. * CSipSimple is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.csipsimple.wizards.impl;
  22. import android.text.InputType;
  23. import android.text.format.DateFormat;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.widget.LinearLayout;
  27. import android.widget.TextView;
  28. import com.csipsimple.R;
  29. import com.csipsimple.api.SipConfigManager;
  30. import com.csipsimple.api.SipProfile;
  31. import com.csipsimple.utils.Log;
  32. import com.csipsimple.utils.MD5;
  33. import com.csipsimple.utils.PreferencesWrapper;
  34. import com.csipsimple.wizards.utils.AccountCreationFirstView;
  35. import com.csipsimple.wizards.utils.AccountCreationFirstView.OnAccountCreationFirstViewListener;
  36. import com.csipsimple.wizards.utils.AccountCreationWebview;
  37. import com.csipsimple.wizards.utils.AccountCreationWebview.OnAccountCreationDoneListener;
  38. import org.apache.http.client.methods.HttpGet;
  39. import org.apache.http.client.methods.HttpRequestBase;
  40. import java.io.IOException;
  41. import java.lang.ref.WeakReference;
  42. import java.util.Date;
  43. public class Ippi extends SimpleImplementation implements OnAccountCreationDoneListener, OnAccountCreationFirstViewListener {
  44. protected static final String THIS_FILE = "IppiW";
  45. private LinearLayout customWizard;
  46. private TextView customWizardText;
  47. private AccountCreationWebview extAccCreator;
  48. private ViewGroup validationBar;
  49. private ViewGroup settingsContainer;
  50. private AccountCreationFirstView firstView;
  51. @Override
  52. protected String getDomain() {
  53. return "ippi.fr";
  54. }
  55. @Override
  56. protected String getDefaultName() {
  57. return "ippi";
  58. }
  59. //Customization
  60. @Override
  61. public void fillLayout(final SipProfile account) {
  62. super.fillLayout(account);
  63. accountUsername.getEditText().setInputType(InputType.TYPE_CLASS_TEXT);
  64. //Get wizard specific row
  65. customWizardText = (TextView) parent.findViewById(R.id.custom_wizard_text);
  66. customWizard = (LinearLayout) parent.findViewById(R.id.custom_wizard_row);
  67. settingsContainer = (ViewGroup) parent.findViewById(R.id.settings_container);
  68. validationBar = (ViewGroup) parent.findViewById(R.id.validation_bar);
  69. updateAccountInfos(account);
  70. extAccCreator = new AccountCreationWebview(parent, "https://m.ippi.fr/subscribe/android.php", this);
  71. }
  72. @Override
  73. public void setDefaultParams(PreferencesWrapper prefs) {
  74. super.setDefaultParams(prefs);
  75. // Add stun server
  76. prefs.setPreferenceBooleanValue(SipConfigManager.ENABLE_STUN, true);
  77. prefs.setPreferenceBooleanValue(SipConfigManager.ENABLE_ICE, false); /* Seems to produce problems with TCP ? -- specific? */
  78. prefs.setPreferenceBooleanValue(SipConfigManager.USE_COMPACT_FORM, true);
  79. prefs.addStunServer("stun.ippi.fr");
  80. }
  81. private void setFirstViewVisibility(boolean visible) {
  82. if(firstView != null) {
  83. firstView.setVisibility(visible ? View.VISIBLE : View.GONE);
  84. }
  85. validationBar.setVisibility(visible ? View.GONE : View.VISIBLE);
  86. settingsContainer.setVisibility(visible ? View.GONE : View.VISIBLE);
  87. }
  88. private void updateAccountInfos(final SipProfile acc) {
  89. if (acc != null && acc.id != SipProfile.INVALID_ID) {
  90. customWizard.setVisibility(View.GONE);
  91. accountBalanceHelper.launchRequest(acc);
  92. } else {
  93. if(firstView == null) {
  94. firstView = new AccountCreationFirstView(parent);
  95. ViewGroup globalContainer = (ViewGroup) settingsContainer.getParent();
  96. firstView.setOnAccountCreationFirstViewListener(this);
  97. globalContainer.addView(firstView);
  98. }
  99. setFirstViewVisibility(true);
  100. }
  101. }
  102. private AccountBalanceHelper accountBalanceHelper = new AccountBalance(this);
  103. private static class AccountBalance extends AccountBalanceHelper {
  104. WeakReference<Ippi> w;
  105. AccountBalance(Ippi wizard){
  106. w = new WeakReference<Ippi>(wizard);
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. @Override
  112. public HttpRequestBase getRequest(SipProfile acc) throws IOException {
  113. String requestURL = "https://soap.ippi.fr/credit/check_credit.php?"
  114. + "login=" + acc.username
  115. + "&code=" + MD5.MD5Hash(acc.data + DateFormat.format("yyyyMMdd", new Date()));
  116. return new HttpGet(requestURL);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. @Override
  122. public String parseResponseLine(String line) {
  123. try {
  124. float value = Float.parseFloat(line.trim());
  125. if (value >= 0) {
  126. return "Credit : " + Math.round(value * 100.0) / 100.0 + " euros";
  127. }
  128. } catch (NumberFormatException e) {
  129. Log.e(THIS_FILE, "Can't get value for line");
  130. }
  131. return null;
  132. }
  133. @Override
  134. public void applyResultError() {
  135. Ippi wizard = w.get();
  136. if(wizard != null) {
  137. wizard.customWizard.setVisibility(View.GONE);
  138. }
  139. }
  140. @Override
  141. public void applyResultSuccess(String balanceText) {
  142. Ippi wizard = w.get();
  143. if(wizard != null) {
  144. wizard.customWizardText.setText(balanceText);
  145. wizard.customWizard.setVisibility(View.VISIBLE);
  146. }
  147. }
  148. };
  149. @Override
  150. protected boolean canTcp() {
  151. return true;
  152. }
  153. @Override
  154. public boolean needRestart() {
  155. return true;
  156. }
  157. public SipProfile buildAccount(SipProfile account) {
  158. account = super.buildAccount(account);
  159. //Proxy useless....?????
  160. //account.proxies = null;
  161. account.vm_nbr = "*1234";
  162. return account;
  163. }
  164. /**
  165. * {@inheritDoc}
  166. */
  167. @Override
  168. public void onAccountCreationDone(String username, String password) {
  169. setUsername(username);
  170. setPassword(password);
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. @Override
  176. public void onAccountCreationDone(String username, String password, String extra) {
  177. onAccountCreationDone(username, password);
  178. }
  179. @Override
  180. public boolean saveAndQuit() {
  181. if(canSave()) {
  182. parent.saveAndFinish();
  183. return true;
  184. }
  185. return false;
  186. }
  187. @Override
  188. public void onCreateAccountRequested() {
  189. setFirstViewVisibility(false);
  190. extAccCreator.show();
  191. }
  192. @Override
  193. public void onEditAccountRequested() {
  194. setFirstViewVisibility(false);
  195. }
  196. }