|
| 1 | +package com.alrubaye.mytracker; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.content.SharedPreferences; |
| 6 | + |
| 7 | +import com.google.firebase.database.DatabaseReference; |
| 8 | +import com.google.firebase.database.FirebaseDatabase; |
| 9 | + |
| 10 | +import java.text.DateFormat; |
| 11 | +import java.text.SimpleDateFormat; |
| 12 | +import java.util.Date; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * Created by hussienalrubaye on 9/26/16. |
| 18 | + */ |
| 19 | + |
| 20 | +public class GlobalInfo { |
| 21 | + public static String PhoneNumber=""; |
| 22 | + public static Map<String,String> MyTrackers=new HashMap<>(); |
| 23 | + |
| 24 | + public static void UpdatesInfo(String UserPhone){ |
| 25 | + DateFormat df= new SimpleDateFormat("yyyy/MM/dd HH:MM:ss"); |
| 26 | + Date date= new Date(); |
| 27 | + DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference(); |
| 28 | + mDatabase.child("Users").child(UserPhone). |
| 29 | + child("Updates").setValue(df.format(date).toString()); |
| 30 | + } |
| 31 | + |
| 32 | + public static String FormatPhoneNumber(String Oldnmber){ |
| 33 | + try{ |
| 34 | + String numberOnly= Oldnmber.replaceAll("[^0-9]", ""); |
| 35 | + if(Oldnmber.charAt(0)=='+') numberOnly="+" +numberOnly ; |
| 36 | + if (numberOnly.length()>=10) |
| 37 | + numberOnly=numberOnly.substring(numberOnly.length()-10,numberOnly.length()); |
| 38 | + return(numberOnly); |
| 39 | + } |
| 40 | + catch (Exception ex){ |
| 41 | + return(" "); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + Context context; |
| 47 | + SharedPreferences ShredRef; |
| 48 | + public GlobalInfo(Context context){ |
| 49 | + this.context=context; |
| 50 | + ShredRef=context.getSharedPreferences("myRef",Context.MODE_PRIVATE); |
| 51 | + } |
| 52 | + |
| 53 | + void SaveData(){ |
| 54 | + String MyTrackersList="" ; |
| 55 | + for (Map.Entry m:GlobalInfo.MyTrackers.entrySet()){ |
| 56 | + if (MyTrackersList.length()==0) |
| 57 | + MyTrackersList=m.getKey() + "%" + m.getValue(); |
| 58 | + else |
| 59 | + MyTrackersList+= m.getKey() + "%" + m.getValue(); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + if (MyTrackersList.length()==0) |
| 64 | + MyTrackersList="empty"; |
| 65 | + |
| 66 | + |
| 67 | + SharedPreferences.Editor editor=ShredRef.edit(); |
| 68 | + editor.putString("MyTrackers",MyTrackersList); |
| 69 | + editor.putString("PhoneNumber",PhoneNumber); |
| 70 | + editor.commit(); |
| 71 | + } |
| 72 | + |
| 73 | + void LoadData(){ |
| 74 | + MyTrackers.clear(); |
| 75 | + String PhoneNumber= ShredRef.getString("PhoneNumber","empty"); |
| 76 | + String MyTrackersList= ShredRef.getString("MyTrackersList","empty"); |
| 77 | + if (!MyTrackersList.equals("empty")){ |
| 78 | + String[] users=MyTrackersList.split("%"); |
| 79 | + for (int i=0;i<users.length;i=i+2){ |
| 80 | + MyTrackers.put(users[i],users[i+1]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + if (PhoneNumber.equals("empty")){ |
| 86 | + |
| 87 | + Intent intent=new Intent(context, Login.class); |
| 88 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 89 | + context.startActivity(intent); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments