From 80972e8c99c833e661f4e63912e9905f42bf3b2f Mon Sep 17 00:00:00 2001 From: Akshat Jain Date: Sun, 28 Jan 2018 22:56:08 +0530 Subject: [PATCH] backup and retrieval added --- app/build.gradle | 3 + app/proguard-rules.pro | 3 + .../motonavigator/activity/BaseActivity.java | 12 +- .../motonavigator/auth/AuthActivity.java | 73 +++++++++++ .../technie/motonavigator/utils/Utility.java | 119 ++++++++++++++++++ app/src/main/res/drawable-v21/ic_backup.png | Bin 0 -> 3085 bytes .../main/res/menu/activity_main_drawer.xml | 5 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable-v21/ic_backup.png diff --git a/app/build.gradle b/app/build.gradle index abdd341..7bcef5f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,10 @@ dependencies { debugCompile 'com.facebook.stetho:stetho:1.3.1' // Firebase Authentication compile 'com.google.firebase:firebase-auth:9.8.0' + compile 'com.google.firebase:firebase-database:9.8.0' // Google Sign In SDK (only required for Google Sign In) compile 'com.google.android.gms:play-services-auth:9.8.0' + + compile 'com.squareup.okhttp3:okhttp:3.4.1' } apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 2fecd43..704ce23 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -15,3 +15,6 @@ #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} +-dontwarn okhttp3.** +-dontwarn okio.** +-dontwarn javax.annotation.** diff --git a/app/src/main/java/work/technie/motonavigator/activity/BaseActivity.java b/app/src/main/java/work/technie/motonavigator/activity/BaseActivity.java index ded8143..06cdf0c 100644 --- a/app/src/main/java/work/technie/motonavigator/activity/BaseActivity.java +++ b/app/src/main/java/work/technie/motonavigator/activity/BaseActivity.java @@ -47,6 +47,7 @@ import work.technie.motonavigator.fragment.DriveCollectionFragment; import work.technie.motonavigator.fragment.DriveFragment; import work.technie.motonavigator.fragment.MapFragment; +import work.technie.motonavigator.utils.Utility; /** * Created by anupam on 31/10/16. @@ -63,6 +64,9 @@ public class BaseActivity extends AppCompatActivity private final String FRAGMENT_TAG_REST = "FTAGR"; private int currentMenuItemId; + private FirebaseAuth mAuth; + private FirebaseUser user; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -70,8 +74,8 @@ protected void onCreate(Bundle savedInstanceState) { NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); - FirebaseAuth mAuth = FirebaseAuth.getInstance(); - FirebaseUser user = mAuth.getCurrentUser(); + mAuth = FirebaseAuth.getInstance(); + user = mAuth.getCurrentUser(); View hView = navigationView.getHeaderView(0); TextView nav_user_name = (TextView) hView.findViewById(R.id.user_name); @@ -163,6 +167,10 @@ private void doMenuAction(int menuItemId) { case R.id.nav_sign_out: signOut(); break; + case R.id.nav_backup: + if(user!=null) { + Utility.backUpData(getApplicationContext(), user.getUid()); + } default: //nothing; } diff --git a/app/src/main/java/work/technie/motonavigator/auth/AuthActivity.java b/app/src/main/java/work/technie/motonavigator/auth/AuthActivity.java index c819983..649ae67 100644 --- a/app/src/main/java/work/technie/motonavigator/auth/AuthActivity.java +++ b/app/src/main/java/work/technie/motonavigator/auth/AuthActivity.java @@ -31,9 +31,11 @@ * limitations under the License. */ import android.app.Activity; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import android.text.TextUtils; @@ -57,8 +59,14 @@ import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.auth.GoogleAuthProvider; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.ValueEventListener; import work.technie.motonavigator.R; +import work.technie.motonavigator.data.MotorContract; /** * Created by anupam on 29/10/16. @@ -191,6 +199,71 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { editor.putInt(getString(R.string.login_mode), GOOGLE_SIGN_IN); editor.apply(); + + final Uri waypointUri = MotorContract.Waypoints.buildWaypointUri(); + this.getContentResolver().delete(waypointUri,null,null); + + final Uri stepsUri = MotorContract.Steps.buildStepUri(); + this.getContentResolver().delete(stepsUri,null,null); + + if(mAuth.getCurrentUser() != null) { + FirebaseDatabase database = FirebaseDatabase.getInstance(); + DatabaseReference waypointsRef = database.getReference(mAuth.getCurrentUser().getUid()) + .child("STEPS"); + waypointsRef.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(DataSnapshot dataSnapshot) { + for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) { + ContentValues contentValue = new ContentValues(); + contentValue.put(MotorContract.Steps.ROUTE_ID, messageSnapshot.child(MotorContract.Steps.ROUTE_ID).getValue().toString()); + contentValue.put(MotorContract.Steps.BEARING_BEFORE, messageSnapshot.child(MotorContract.Steps.BEARING_BEFORE).getValue().toString()); + contentValue.put(MotorContract.Steps.BEARING_AFTER, messageSnapshot.child(MotorContract.Steps.BEARING_AFTER).getValue().toString()); + contentValue.put(MotorContract.Steps.LOCATION_LAT, messageSnapshot.child(MotorContract.Steps.LOCATION_LAT).getValue().toString()); + contentValue.put(MotorContract.Steps.LOCATION_LONG, messageSnapshot.child(MotorContract.Steps.LOCATION_LONG).getValue().toString()); + contentValue.put(MotorContract.Steps.TYPE, messageSnapshot.child(MotorContract.Steps.TYPE).getValue().toString()); + contentValue.put(MotorContract.Steps.INSTRUCTION, messageSnapshot.child(MotorContract.Steps.INSTRUCTION).getValue().toString()); + contentValue.put(MotorContract.Steps.MODE, messageSnapshot.child(MotorContract.Steps.MODE).getValue().toString()); + contentValue.put(MotorContract.Steps.DURATION, messageSnapshot.child(MotorContract.Steps.DURATION).getValue().toString()); + contentValue.put(MotorContract.Steps.NAME, messageSnapshot.child(MotorContract.Steps.NAME).getValue().toString()); + contentValue.put(MotorContract.Steps.DISTANCE, messageSnapshot.child(MotorContract.Steps.DISTANCE).getValue().toString()); + + getApplicationContext().getContentResolver().insert(stepsUri, contentValue); + } + } + + @Override + public void onCancelled(DatabaseError firebaseError) { + } + }); + + DatabaseReference stepRef = database.getReference(mAuth.getCurrentUser().getUid()) + .child("WAYPOINTS"); + stepRef.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(DataSnapshot dataSnapshot) { + for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) { + ContentValues contentValue = new ContentValues(); + contentValue.put(MotorContract.Waypoints.ROUTE_ID, messageSnapshot.child(MotorContract.Waypoints.ROUTE_ID).getValue().toString()); + contentValue.put(MotorContract.Waypoints.START_NAME, messageSnapshot.child(MotorContract.Waypoints.START_NAME).getValue().toString()); + contentValue.put(MotorContract.Waypoints.START_LAT, messageSnapshot.child(MotorContract.Waypoints.START_LAT).getValue().toString()); + contentValue.put(MotorContract.Waypoints.START_LONG, messageSnapshot.child(MotorContract.Waypoints.START_LONG).getValue().toString()); + contentValue.put(MotorContract.Waypoints.DEST_NAME, messageSnapshot.child(MotorContract.Waypoints.DEST_NAME).getValue().toString()); + contentValue.put(MotorContract.Waypoints.DEST_LAT, messageSnapshot.child(MotorContract.Waypoints.DEST_LAT).getValue().toString()); + contentValue.put(MotorContract.Waypoints.DEST_LONG, messageSnapshot.child(MotorContract.Waypoints.DEST_LONG).getValue().toString()); + contentValue.put(MotorContract.Waypoints.MODE, messageSnapshot.child(MotorContract.Waypoints.MODE).getValue().toString()); + contentValue.put(MotorContract.Waypoints.ROUTE_DURATION, messageSnapshot.child(MotorContract.Waypoints.ROUTE_DURATION).getValue().toString()); + contentValue.put(MotorContract.Waypoints.ROUTE_DISTANCE, messageSnapshot.child(MotorContract.Waypoints.ROUTE_DISTANCE).getValue().toString()); + + getApplicationContext().getContentResolver().insert(waypointUri, contentValue); + } + } + + @Override + public void onCancelled(DatabaseError firebaseError) { + } + }); + } + Intent intent = new Intent(this, work.technie.motonavigator.activity.BaseActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(intent); diff --git a/app/src/main/java/work/technie/motonavigator/utils/Utility.java b/app/src/main/java/work/technie/motonavigator/utils/Utility.java index 9037a29..5e096fb 100644 --- a/app/src/main/java/work/technie/motonavigator/utils/Utility.java +++ b/app/src/main/java/work/technie/motonavigator/utils/Utility.java @@ -14,9 +14,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import android.app.ProgressDialog; import android.content.Context; +import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.net.Uri; +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import work.technie.motonavigator.data.MotorContract; /** @@ -27,6 +47,37 @@ public class Utility { private static final String WIFI = "WIFI"; private static final String MOBILE = "MOBILE"; + private static final String[] WAYPOINTS_COLUMNS = { + + MotorContract.Waypoints.TABLE_NAME + "." + MotorContract.Waypoints._ID, + MotorContract.Waypoints.START_NAME, + MotorContract.Waypoints.START_LAT, + MotorContract.Waypoints.START_LONG, + MotorContract.Waypoints.DEST_NAME, + MotorContract.Waypoints.DEST_LAT, + MotorContract.Waypoints.DEST_LONG, + MotorContract.Waypoints.MODE, + MotorContract.Waypoints.ROUTE_ID, + MotorContract.Waypoints.ROUTE_DURATION, + MotorContract.Waypoints.ROUTE_DISTANCE + }; + + private static final String[] STEPS_COLUMNS = { + + MotorContract.Steps.TABLE_NAME + "." + MotorContract.Waypoints._ID, + MotorContract.Steps.ROUTE_ID, + MotorContract.Steps.BEARING_BEFORE, + MotorContract.Steps.BEARING_AFTER, + MotorContract.Steps.LOCATION_LAT, + MotorContract.Steps.LOCATION_LONG, + MotorContract.Steps.TYPE, + MotorContract.Steps.INSTRUCTION, + MotorContract.Steps.MODE, + MotorContract.Steps.DURATION, + MotorContract.Steps.NAME, + MotorContract.Steps.DISTANCE + }; + public static boolean hasNetworkConnection(Context context) { boolean hasConnectedWifi = false; boolean hasConnectedMobile = false; @@ -43,4 +94,72 @@ public static boolean hasNetworkConnection(Context context) { } return hasConnectedWifi || hasConnectedMobile; } + + public static void backUpData(Context mContext,String uid) { + Uri waypointUri = MotorContract.Waypoints.buildWaypointUri(); + Cursor waypointCursor = mContext.getContentResolver().query(waypointUri, WAYPOINTS_COLUMNS, null, null, null); + + try { + putToFirebase(waypointCursor,uid,"WAYPOINTS"); + } catch (JSONException e) { + e.printStackTrace(); + } + + Uri stepsUri = MotorContract.Steps.buildStepUri(); + Cursor stepsCursor = mContext.getContentResolver().query(stepsUri, STEPS_COLUMNS, null, null, null); + + try { + putToFirebase(stepsCursor,uid,"STEPS"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + private static void putToFirebase(Cursor cursor,String uid,String tableName) throws JSONException { + + int columnCount = cursor.getColumnCount(); + cursor.moveToFirst(); + do { + JSONObject row = new JSONObject(); + for (int index = 1; index < columnCount; index++) { + row.put(cursor.getColumnName(index), cursor.getString(index)); + } + new OkHttpHandler().execute(row.toString(),uid,tableName,cursor.getString(0)); + } while (cursor.moveToNext()); + + cursor.close(); + } + + private static class OkHttpHandler extends AsyncTask { + + private final OkHttpClient client = new OkHttpClient(); + private final MediaType JSON + = MediaType.parse("application/json; charset=utf-8"); + + @Override + protected void onPreExecute() { + super.onPreExecute(); + } + + @Override + protected byte[] doInBackground(String... params) { + + RequestBody body = RequestBody.create(JSON, params[0]); + Request request = new Request.Builder() + .url("https://moto-navigator-74a33.firebaseio.com/" + params[1] + "/" + params[2]+ "/" + params[3] + ".json") + .put(body) + .build(); + try { + Response response = client.newCall(request).execute(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(byte[] bytes) { + super.onPostExecute(bytes); + } + } } diff --git a/app/src/main/res/drawable-v21/ic_backup.png b/app/src/main/res/drawable-v21/ic_backup.png new file mode 100644 index 0000000000000000000000000000000000000000..43c88e1897af70d53110de28d9590408460f1580 GIT binary patch literal 3085 zcma)8c{tQ<7yb>&mh>hh#VQeWojgcD4m^6}_7em&J!q}Ci zh(d%2V+*e=(=sH+I@!K?|NQ>=uIoF`^<4M4&pG$GpFf|(ix(`!M2?9703c>%X?h6& zK)e(L2<_v+`Yn%NJlN-V&h8ulR6P>i_7vdp<2cJpb^s8q1OTz|0Pu%biv0lqky-%2 z_5c7R4FIG=vR>O8@EUwxHWsD;Q1b8;oX3PiEM3BRP9y(b(D~C(qj*KZ2rD}?!5NU; zfy3Zu5S`BeATn-cdd?y0%dbiI5QjCHj%Cem8xJ8N`6&MlTC`*Eh3gW6 zLy7HBuvmyHPUe%!f!)T1+bi4Nr(DP0JJwc`pf`M*k!wYl!}yt639uv|b>-Ae*-QfU zGSj}pyqZ02T5wqQYm5f&01m-OY7UoX5FRU!<;iD7`W{7nx2yi^L$ojK`ANuGVcq*r zUChJj?ZFQ>8gl#DCtNQ;o-gZ(!BXD_md==$Fk8`e`P*^meTBgfAJN*i)^z9?vF7R6 zmz&Vg_>Rpd(a3?qO#LyaX3V(1mxf=-3q^2AeRekIF8&Ca(1sT5XMa3U`%!e#MJEih zGc@Q>tl`&z+ zQjjA$v@>C;q|wygLVIv%P_86jm{9ZLO(=~qgaJRhqY|}RXUacy5siFY6H zu2eS`T{sVQ*W67`{`N8#MSCkvwCGr@#?S;qMseroTg+j`+FW zs+BZZdPzAy+S$@C z{7laNCbRK_gLeYPu@d!3Hi+DClc=ql>>)Q{ zb>SibEbirEkw?Sj6}v@T;0?xxq%6bedgrXOVqf8za!h)eTr@m`8&^7=+PqyhGB0+Z zNI=1Yzw4*Lq7OOd7)6EX57j#7{{GUMhiW%#(8-$H8W5*EL{c|{_%7APu&p(Tme9{_ z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mxGGl@j19k>?^~oSWm5E|3zCWE`#D$+8s=z58c8wC9S4Gzzh+P(2veYrh^6!@8!K({%G#Awik(L37p;~nnm1pXC$Cb4wM!rI}!kJ~?YHpEG##fU7)tkT}e z*MVxd&AHKF{B0&tl+1Cj7hu8Cq=P)b7_~67>D5knwyyqLM&-s1b_RY`H^l>A zeR|5BRKYNOqL@`KpP1@#VuaOPLn955IqC(>f~u2co(6&|Q|>**5C}ZZhrR4uRv4^|HjZ-h1pdu(#IMKe|5Bd$9%zYDe7NOt_hV% z=+`F)+jqB};y(?$wxSpw||WG#vzXAzBW?2QWC5Sx*XQO`|?_;96MY&M{L zccROh8$nmYdRgPGrP^UlHJkE-2oq-f&t+lJjLAvT?PtiM@q5c|t)}MO%Funs2$>BA zm}H5K0*$^yO7O>4Su)*i4zEu&zQ2@KeV3p0CpFa2hP6g{ttNv?G#Y%j+0{N@d%fL7yGd6Vn1mz^CvCi{|p!At~i?t&iJyp{WtID zs~BPC9O3O5;e*75`S1XMYr*uOS}-VF&jAKU!t{`Egqju%sin2DeRT4_1cAZc*L(^8 zTW~i&&yy#R`PTsx>>Gji4D$i-c)Z57pzGl{&k!Gt;4r_Ziw4JdLx7dp1=I5L9(Vr( DO#{fk literal 0 HcmV?d00001 diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index cb6881a..71aaa94 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -28,6 +28,11 @@ android:id="@+id/nav_directions" android:icon="@drawable/ic_navigation_black_36dp" android:title="@string/drive" /> + + Sign Out Start Navigation Toogle Location Service + Backup Data