Commit edd209be366817fbaa64900ca9a935284670ec51

Authored by JLo'w
1 parent 814b5a6f

URL encode + preference gestion serveur

PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java
... ... @@ -3,16 +3,19 @@ package com.example.app_10p5;
3 3 import android.app.Activity;
4 4 import android.app.PendingIntent;
5 5 import android.content.Intent;
  6 +import android.content.SharedPreferences;
6 7 import android.graphics.drawable.ColorDrawable;
7 8 import android.nfc.NfcAdapter;
8 9 import android.nfc.Tag;
9 10 import android.nfc.tech.MifareClassic;
10 11 import android.os.Bundle;
  12 +import android.preference.PreferenceManager;
11 13 import android.widget.Toast;
12 14  
13 15 import org.json.JSONObject;
14 16  
15 17 import java.net.URL;
  18 +import java.net.URLEncoder;
16 19 import java.util.HashMap;
17 20  
18 21 /**
... ... @@ -24,8 +27,6 @@ public class CarteActivite extends Activity implements ASyncResponse {
24 27 private HashMap<String, String> mParam;
25 28 private String mAPI;
26 29  
27   - public static String HOST = "https://10p5.clubinfo.frogeye.fr/";
28   -
29 30 @Override
30 31 protected void onCreate(Bundle savedInstanceState) {
31 32 super.onCreate(savedInstanceState);
... ... @@ -34,35 +35,40 @@ public class CarteActivite extends Activity implements ASyncResponse {
34 35  
35 36 mParam = new HashMap<String, String>();
36 37  
37   - switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) {
38   - case MainActivite.STATE_COMMANDE:
39   - //TODO: XOR du cancer
40   - //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1)));
41   - mParam.put("montant", String.valueOf(getIntent().getFloatExtra("montant", -1)));
42   - mParam.put("jeton", getIntent().getStringExtra("token"));
43   - mAPI = "api/client/payer";
44   - break;
45   - case MainActivite.STATE_CREATION_COMPTE:
46   - mParam.put("solde", String.valueOf(getIntent().getFloatExtra("montant", -1)));
47   - mParam.put("jeton", getIntent().getStringExtra("token"));
48   - mAPI = "api/client/ajouter";
49   - break;
50   - case MainActivite.STATE_RECHARGEMENT:
51   - System.out.println("bite");
52   - mParam.put("montant", String.valueOf(getIntent().getFloatExtra("montant", -1)));
53   - mParam.put("jeton", getIntent().getStringExtra("token"));
54   - mAPI = "api/client/recharger";
55   - break;
56   - case MainActivite.STATE_VIDANGE:
57   - mParam.put("jeton", getIntent().getStringExtra("token"));
58   - mAPI = "api/client/vidange";
59   - break;
60   - case MainActivite.STATE_CONNEXION: //Impossible c'est pas géré ici
61   - case MainActivite.STATE_RIEN:
62   - default:
63   - Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
64   - finish();
65   - return;
  38 + try {
  39 + switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) {
  40 + case MainActivite.STATE_COMMANDE:
  41 + //TODO: XOR du cancer
  42 + //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1)));
  43 + mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
  44 + mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
  45 + mAPI = "api/client/payer";
  46 + break;
  47 + case MainActivite.STATE_CREATION_COMPTE:
  48 + mParam.put("solde", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
  49 + mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
  50 + mAPI = "api/client/ajouter";
  51 + break;
  52 + case MainActivite.STATE_RECHARGEMENT:
  53 + System.out.println("bite");
  54 + mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
  55 + mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
  56 + mAPI = "api/client/recharger";
  57 + break;
  58 + case MainActivite.STATE_VIDANGE:
  59 + mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
  60 + mAPI = "api/client/vidange";
  61 + break;
  62 + case MainActivite.STATE_CONNEXION: //Impossible c'est pas géré ici
  63 + case MainActivite.STATE_RIEN:
  64 + default:
  65 + Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
  66 + finish();
  67 + return;
  68 + }
  69 + }
  70 + catch (Throwable t){
  71 + System.out.println("Exception: " + t.toString());
66 72 }
67 73  
68 74 mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
... ... @@ -182,7 +188,8 @@ public class CarteActivite extends Activity implements ASyncResponse {
182 188  
183 189 public void clientAPI() {
184 190 try {
185   - URL url = new URL(HOST + mAPI);
  191 + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  192 + URL url = new URL(settings.getString("server_address", null) + mAPI);
186 193 NetworkThread nt = new NetworkThread(url, mParam);
187 194 nt.delegate = this;
188 195 nt.execute();
... ...
PremiereActivite/app/src/main/java/com/example/app_10p5/SettingsFragment.java 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +package com.example.app_10p5;
  2 +
  3 +import android.os.Bundle;
  4 +import android.preference.PreferenceFragment;
  5 +
  6 +/**
  7 + * Created by beaus on 05/05/2016.
  8 + */
  9 +public class SettingsFragment extends PreferenceFragment {
  10 + @Override
  11 + public void onCreate(Bundle savedInstanceState) {
  12 + super.onCreate(savedInstanceState);
  13 +
  14 + // Load the preferences from an XML resource
  15 + addPreferencesFromResource(R.xml.preferences);
  16 + }
  17 +}
... ...
PremiereActivite/app/src/main/java/com/example/app_10p5/main_tab_frag.java
... ... @@ -41,12 +41,13 @@ public class main_tab_frag extends Fragment {
41 41 tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
42 42  
43 43 final ViewPager viewPager = (ViewPager) ret.findViewById(R.id.pager);
44   - final PagerAdapter adapter = new PagerAdapter(getFragmentManager(), tabLayout.getTabCount());
  44 + final PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
45 45 viewPager.setAdapter(adapter);
46 46 viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
47 47 tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
48 48 @Override
49 49 public void onTabSelected(TabLayout.Tab tab) {
  50 +
50 51 if(parent.getToken() != "" && System.currentTimeMillis() - parent.getTimeToken() < MainActivite.EXPIRATION){
51 52 viewPager.setCurrentItem(tab.getPosition());
52 53 }
... ... @@ -67,7 +68,6 @@ public class main_tab_frag extends Fragment {
67 68  
68 69 }
69 70 });
70   -
71 71 return ret;
72 72 }
73 73  
... ...
PremiereActivite/app/src/main/res/menu/menu.xml
... ... @@ -19,12 +19,7 @@
19 19 android:id="@+id/action_settings"
20 20 android:title="@string/action_settings"
21 21 android:showAsAction="never"
22   - app:showAsAction="never"><menu>
23   - <group android:checkableBehavior="single">
24   - <item android:id="@+id/menu_2_choice_1" android:title="Geoffrey" android:showAsAction="never" android:checked="true" />
25   - <item android:id="@+id/menu_2_choice_2" android:title="Bar" android:showAsAction="never" />
26   - </group>
27   -</menu>
  22 + app:showAsAction="never">
28 23 </item>
29 24  
30 25  
... ...
PremiereActivite/app/src/main/res/values/strings.xml
... ... @@ -11,8 +11,17 @@
11 11 <string name="passCarte">Passez la carte du client</string>
12 12 <string name="disconnect">Déconnexion</string>
13 13 <string name="connect">Connexion</string>
14   -
15   - <!-- TODO: Remove or change this placeholder text -->
16   - <string name="hello_blank_fragment">Hello blank fragment</string>
  14 + <string name="server">Serveur</string>
  15 + <string name="serverTest">Serveur Test</string>
  16 + <string name="serverBar">Serveur Bar</string>
  17 +
  18 + <array name="server_entries">
  19 + <item>Polytech Bar</item>
  20 + <item>Beta Test</item>
  21 + </array>
  22 + <array name="server_values">
  23 + <item>http://pcbar.insecure.deule.net/</item>
  24 + <item>https://10p5.clubinfo.frogeye.fr/</item>
  25 + </array>
17 26  
18 27 </resources>
... ...
PremiereActivite/app/src/main/res/xml/preferences.xml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  3 +
  4 + <ListPreference
  5 + android:defaultValue="http://pcbar.insecure.deule.net/"
  6 + android:title="@string/server"
  7 + android:key="server_address"
  8 + android:entries="@array/server_entries"
  9 + android:entryValues="@array/server_values"/>
  10 +
  11 +</PreferenceScreen>
0 12 \ No newline at end of file
... ...