Advertisement
thongz

BaseActivity

Oct 24th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. import android.content.ContentValues;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.database.Cursor;
  5. import android.graphics.Bitmap;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10.  
  11. import com.younginnovators.sjk.customcontrols.MessageDialog;
  12. import com.younginnovators.sjk.databaseandpreferences.SharedPref;
  13. import com.younginnovators.sjk.databaseandpreferences.ZayRideDatabase;
  14. import com.younginnovators.sjk.databaseandpreferences.ZayRideDatabaseUtility;
  15.  
  16. import java.util.Locale;
  17.  
  18.  
  19. /**
  20.  * Created by alone on image3/17/2016.
  21.  */
  22. public class BaseActivity extends AppCompatActivity {
  23.     public SharedPref prefs;
  24.     public Resources resources;
  25.     private Bitmap bitmap;
  26.     public double lat;
  27.     public double lang;
  28.     public MessageDialog messageDialog;
  29.     private Locale myLocale;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         prefs = new SharedPref();
  35.         resources = getResources();
  36.         messageDialog = MessageDialog.getInstance();
  37.         //setLocale();
  38.     }
  39.  
  40.     //public void setLocale() {
  41.  
  42.         //myLocale = new Locale("zh");
  43.         //Resources res = getResources();
  44.         //DisplayMetrics dm = res.getDisplayMetrics();
  45.         //Configuration conf = res.getConfiguration();
  46.         //conf.locale = myLocale;
  47.         //res.updateConfiguration(conf, dm);
  48.         //// Intent refresh = new Intent(this, AndroidLocalize.class);
  49.         //// startActivity(refresh);
  50.  
  51.  
  52.     //}
  53.  
  54.     /**
  55.      * Get the device id from database.
  56.      */
  57.     public String getDeviceIdFromDatabase() {
  58.         String deviceId = "";
  59.         ZayRideDatabase database = new ZayRideDatabase(this);
  60.         database.openDatabase();
  61.         Cursor cursor = database.getData("Select " + ZayRideDatabaseUtility.DEVICE_ID + " from " + ZayRideDatabaseUtility.TABLE_DEVICE_INFO);
  62.         if (cursor.getCount() > 0 && cursor.moveToFirst()) {
  63.             deviceId = cursor.getString(cursor.getColumnIndex(ZayRideDatabaseUtility.DEVICE_ID));
  64.         }
  65.         cursor.close();
  66.         database.closeDatabase();
  67.  
  68.         return deviceId;
  69.     }
  70.  
  71.     protected boolean isNetworkAvilable() {
  72.         boolean isNetworkAvilable = false;
  73.         ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  74.         NetworkInfo info = manager.getActiveNetworkInfo();
  75.         if (info != null && info.isAvailable() && info.isConnected()) {
  76.             isNetworkAvilable = true;
  77.         }
  78.         return isNetworkAvilable;
  79.     }
  80.  
  81.     /**
  82.      * Method to update the status of already login after successful login status is one and after signuout status is 0.
  83.      *
  84.      * @param status value to e updated in interger image1 for login and 0 for not login
  85.      */
  86.     protected void updateAlreadyLoginStatus(int status) {
  87.  
  88.         ZayRideDatabase database = new ZayRideDatabase(this);
  89.         database.openDatabase();
  90.         ContentValues values = new ContentValues();
  91.         values.put(ZayRideDatabaseUtility.Configuration.COLUMN_NAME_IS_ALREADY_LOGGED_IN, status);
  92.         if (checkConfigurationTableDataExixtanceStatus()) {
  93.             database.updateData(values, ZayRideDatabaseUtility.Configuration.TABLE_NAME);
  94.  
  95.         } else {
  96.             database.insertData(values, ZayRideDatabaseUtility.Configuration.TABLE_NAME);
  97.         }
  98.         database.close();
  99.  
  100.     }
  101.  
  102.     /**
  103.      * check the database for data status if has record or not
  104.      *
  105.      * @return boolean value true if table  already containdata
  106.      */
  107.     protected boolean checkConfigurationTableDataExixtanceStatus() {
  108.  
  109.         boolean isAlreadyContainData = false;
  110.         ZayRideDatabase database = new ZayRideDatabase(this);
  111.         database.openDatabase();
  112.  
  113.         Cursor cursor = database.getData("Select * from " + ZayRideDatabaseUtility.Configuration.TABLE_NAME);
  114.         if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
  115.             isAlreadyContainData = true;
  116.  
  117.         }
  118.         cursor.close();
  119.         database.close();
  120.         return isAlreadyContainData;
  121.     }
  122.  
  123.     /**
  124.      * check the database for login status if user is alreadylogin then redirect it to home
  125.      *
  126.      * @return boolean value true if user is already login
  127.      */
  128.     protected boolean checkAlreadyLoginStatus() {
  129.  
  130.         boolean isAlreadyLoggin = false;
  131.         ZayRideDatabase database = new ZayRideDatabase(this);
  132.         database.openDatabase();
  133.  
  134.         Cursor cursor = database.getData("Select * from " + ZayRideDatabaseUtility.Configuration.TABLE_NAME);
  135.         if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
  136.  
  137.             do {
  138.                 int status = cursor.getInt(cursor.getColumnIndex(ZayRideDatabaseUtility.Configuration.COLUMN_NAME_IS_ALREADY_LOGGED_IN));
  139.                 if (status == 1) {
  140.                     isAlreadyLoggin = true;
  141.                 }
  142.             } while (cursor.moveToNext());
  143.  
  144.         }
  145.         cursor.close();
  146.         database.close();
  147.         return isAlreadyLoggin;
  148.     }
  149.  
  150.     protected boolean checkProfileStatus() {
  151.  
  152.         boolean isAlreadyLoggin = false;
  153.         ZayRideDatabase database = new ZayRideDatabase(this);
  154.         database.openDatabase();
  155.  
  156.         Cursor cursor = database.getData("Select * from " + ZayRideDatabaseUtility.ProfileDetail.TABLE_NAME);
  157.         if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
  158.  
  159.             isAlreadyLoggin = true;
  160.         }
  161.         cursor.close();
  162.         database.close();
  163.         return isAlreadyLoggin;
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Get the device id from database.
  169.      */
  170.     public int getUserIdFromDatabase() {
  171.         int userId = 0;
  172.         ZayRideDatabase database = new ZayRideDatabase(this);
  173.         database.openDatabase();
  174.         Cursor cursor = database.getData("Select " + ZayRideDatabaseUtility.USER_ID + " from " + ZayRideDatabaseUtility.TABLE_LOGIN_DATA);
  175.         if (cursor.getCount() > 0 && cursor.moveToFirst()) {
  176.             userId = cursor.getInt(cursor.getColumnIndex(ZayRideDatabaseUtility.USER_ID));
  177.         }
  178.         cursor.close();
  179.         database.closeDatabase();
  180.  
  181.         return userId;
  182.     }
  183.  
  184.     /**
  185.      * get the coordinates of current location from database
  186.      *
  187.      * @return
  188.      */
  189. //    protected MyLocationCoordinates getCoordinates() {
  190. //        MyLocationCoordinates coordinates = new MyLocationCoordinates();
  191. //        ZayRideDatabase database = new ZayRideDatabase(this);
  192. //        database.openDatabase();
  193. //
  194. //        Cursor mCursor = database.getData("Select * from " + ZayRideDatabaseUtility.MyLocation.TABLE_NAME);
  195. //        mCursor.moveToFirst();
  196. //        if (mCursor != null && mCursor.getCount() > 0) {
  197. //            do {
  198. //
  199. //                double latitude = mCursor.getDouble(mCursor.getColumnIndex(ZayRideDatabaseUtility.MyLocation.COLUMN_NAME_LATITUDE));
  200. //                double longitude = mCursor.getDouble(mCursor.getColumnIndex(ZayRideDatabaseUtility.MyLocation.COLUMN_NAME_LONGITUDE));
  201. //                coordinates.setLatitude(latitude);
  202. //                coordinates.setLongitude(longitude);
  203. //            } while (mCursor.moveToNext());
  204. //        }
  205. //        mCursor.close();
  206. //        database.close();
  207. //        return coordinates;
  208. //    }
  209.  
  210.     /**
  211.      * Insert the current location coordinates in dadabase after getting from location service
  212.      *
  213.      * @param mLatitude
  214.      * @param mLongitude
  215.      */
  216.     protected void insertLocationCoordinatesInDatabase(double mLatitude, double mLongitude) {
  217.  
  218.         ZayRideDatabase database = new ZayRideDatabase(this);
  219.         database.openDatabase();
  220.         database.deleteData(ZayRideDatabaseUtility.MyLocation.TABLE_NAME);
  221.         ContentValues values = new ContentValues();
  222.         values.put(ZayRideDatabaseUtility.MyLocation.COLUMN_NAME_LATITUDE, mLatitude);
  223.         values.put(ZayRideDatabaseUtility.MyLocation.COLUMN_NAME_LONGITUDE, mLongitude);
  224.  
  225.         database.insertData(values, ZayRideDatabaseUtility.MyLocation.TABLE_NAME);
  226.  
  227.     }
  228.  
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement