Created
June 30, 2014 05:15
-
-
Save nishitpatel/bc9cb73bb6f298b9312f to your computer and use it in GitHub Desktop.
Create DBHelper,database Function and adapter class in android.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.applockfree.adapters; | |
import java.util.ArrayList; | |
import android.content.Context; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.graphics.drawable.Drawable; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.vs2.applockfree.databases.DatabaseFunctions; | |
import com.vs2.applockfree.objects.ApplicationInformation; | |
import com.vs2.applockfree.R; | |
public class ApplicationListAdpater extends BaseAdapter { | |
ArrayList<ApplicationInformation> mApplicationList; | |
LayoutInflater mInflater; | |
Context mContext; | |
public ApplicationListAdpater(Context context, | |
ArrayList<ApplicationInformation> applicationlist) { | |
// TODO Auto-generated constructor stub | |
mContext = context; | |
mInflater = LayoutInflater.from(mContext); | |
this.mApplicationList = applicationlist; | |
} | |
@Override | |
public int getCount() { | |
// TODO Auto-generated method stub | |
return mApplicationList.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
// TODO Auto-generated method stub | |
return mApplicationList.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
// TODO Auto-generated method stub | |
return position; | |
} | |
@SuppressWarnings({ "deprecation" }) | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
// TODO Auto-generated method stub | |
final ViewHolder holder; | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.application_list_item, | |
null); | |
holder = new ViewHolder(); | |
holder.imageViewIcon = (ImageView) convertView | |
.findViewById(R.id.imageViewIcon); | |
holder.imageViewSecure = (ImageView) convertView | |
.findViewById(R.id.imageViewSecure); | |
holder.textAppName = (TextView) convertView | |
.findViewById(R.id.textViewAppName); | |
holder.textSummary = (TextView) convertView | |
.findViewById(R.id.textViewSummary); | |
/*holder.chBoxSelect = (CheckBox) convertView | |
.findViewById(R.id.checkBoxselect);*/ | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
Drawable icon = null; | |
try { | |
icon = mContext.getPackageManager().getApplicationIcon( | |
mApplicationList.get(position).getPackageName()); | |
} catch (NameNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (icon != null) { | |
holder.imageViewIcon.setImageDrawable(icon); | |
} else { | |
holder.imageViewIcon.setBackgroundResource(R.drawable.ic_launcher); | |
} | |
/* String appName = "<b>" | |
+ mApplicationList.get(position).getApplicationName() + "</b> ( "+mApplicationList.get(position).getVersion()+" )"; | |
Spanned sp = Html.fromHtml(appName); | |
*/ | |
holder.textAppName.setText(""+mApplicationList.get(position).getApplicationName()); | |
holder.textSummary.setText("" | |
+ mApplicationList.get(position).getPackageName()); | |
ApplicationInformation applicationInfo = DatabaseFunctions.isApplicationProtected(mApplicationList.get(position).getPackageName()); | |
if(applicationInfo != null){ | |
if(applicationInfo.isIndividualSecure()){ | |
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.light_pink)); | |
holder.imageViewSecure.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_individual_secure)); | |
}else{ | |
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.light_red)); | |
holder.imageViewSecure.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_secure)); | |
} | |
}else{ | |
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white)); | |
holder.imageViewSecure.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_not_secure)); | |
} | |
return convertView; | |
} | |
public class ViewHolder { | |
ImageView imageViewIcon,imageViewSecure; | |
TextView textAppName, textSummary; | |
//CheckBox chBoxSelect; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.applockfree.databases; | |
import java.util.ArrayList; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import com.vs2.applockfree.objects.ApplicationInformation; | |
import com.vs2.applockfree.objects.Globals; | |
import com.vs2.applockfree.utilities.Logcat; | |
public class DatabaseFunctions { | |
public static SQLiteDatabase db; | |
public static DBHelper dbHelper; | |
public static void openDB(Context context) { | |
dbHelper = new DBHelper(context); | |
if (db != null) { | |
if (!db.isOpen()) { | |
db = dbHelper.getWritableDatabase(); | |
} | |
} else { | |
db = dbHelper.getWritableDatabase(); | |
} | |
} | |
public static void closeDB() { | |
if (db.isOpen()) { | |
db.close(); | |
dbHelper.close(); | |
} | |
} | |
// Application Master | |
public static ArrayList<ApplicationInformation> getProtectedApplication() { | |
ArrayList<ApplicationInformation> applicationList = new ArrayList<ApplicationInformation>(); | |
Cursor cursor = null; | |
try { | |
cursor = db.rawQuery("SELECT " + ApplicationInformation._Id + "," | |
+ ApplicationInformation._applicationName + "," | |
+ ApplicationInformation._packageName + "," | |
+ ApplicationInformation._password + ", " | |
+ ApplicationInformation._className + ", " | |
+ ApplicationInformation._individualSecure + " FROM " | |
+ DBHelper.TABLE_APPLICATION, null); | |
while (cursor.moveToNext()) { | |
boolean individualSecure = false; | |
if (cursor.getInt(5) == Globals.TRUE) { | |
individualSecure = true; | |
} else { | |
individualSecure = false; | |
} | |
ApplicationInformation applicationInfo = new ApplicationInformation( | |
cursor.getString(1), cursor.getString(2), | |
cursor.getString(3), null, cursor.getString(4), true, | |
individualSecure); | |
applicationList.add(applicationInfo); | |
} | |
return applicationList; | |
} catch (Exception e) { | |
Logcat.e("Templates SELECT", "error : " + e.toString()); | |
return applicationList; | |
} finally { | |
if (cursor != null && !cursor.isClosed()) { | |
cursor.close(); | |
} | |
} | |
} | |
public static ApplicationInformation isApplicationProtected( | |
String packageName) { | |
Cursor cursor = null; | |
try { | |
cursor = db.rawQuery("SELECT " + ApplicationInformation._Id + "," | |
+ ApplicationInformation._applicationName + "," | |
+ ApplicationInformation._packageName + "," | |
+ ApplicationInformation._password + ", " | |
+ ApplicationInformation._className + ", " | |
+ ApplicationInformation._individualSecure + " FROM " | |
+ DBHelper.TABLE_APPLICATION + " where " | |
+ ApplicationInformation._packageName + " = '" | |
+ packageName + "'", null); | |
while (cursor.moveToNext()) { | |
boolean individualSecure = false; | |
if (cursor.getInt(5) == Globals.TRUE) { | |
individualSecure = true; | |
} else { | |
individualSecure = false; | |
} | |
ApplicationInformation applicationInfo = new ApplicationInformation( | |
cursor.getString(1), cursor.getString(2), | |
cursor.getString(3), null, cursor.getString(4), true, | |
individualSecure); | |
return applicationInfo; | |
} | |
return null; | |
} catch (Exception e) { | |
Logcat.e("Templates SELECT", "error : " + e.toString()); | |
return null; | |
} finally { | |
if (cursor != null && !cursor.isClosed()) { | |
cursor.close(); | |
} | |
} | |
} | |
public static boolean isValidPackagePassword(String packageName, | |
String password) { | |
Cursor cursor = null; | |
try { | |
cursor = db.rawQuery("SELECT " + ApplicationInformation._Id + "," | |
+ ApplicationInformation._applicationName + "," | |
+ ApplicationInformation._packageName + "," | |
+ ApplicationInformation._password + " FROM " | |
+ DBHelper.TABLE_APPLICATION + " where " | |
+ ApplicationInformation._packageName + " = '" | |
+ packageName + "' AND " + ApplicationInformation._password | |
+ " = " + password, null); | |
while (cursor.moveToNext()) { | |
return true; | |
} | |
return false; | |
} catch (Exception e) { | |
Logcat.e("Templates SELECT", "error : " + e.toString()); | |
return false; | |
} finally { | |
if (cursor != null && !cursor.isClosed()) { | |
cursor.close(); | |
} | |
} | |
} | |
public static String getPasswordFromPackageName(String packageName) { | |
Cursor cursor = null; | |
try { | |
cursor = db.rawQuery("SELECT " + ApplicationInformation._Id + "," | |
+ ApplicationInformation._applicationName + "," | |
+ ApplicationInformation._packageName + "," | |
+ ApplicationInformation._password + " FROM " | |
+ DBHelper.TABLE_APPLICATION + " where " | |
+ ApplicationInformation._packageName + " = '" | |
+ packageName+"'", null); | |
while (cursor.moveToNext()) { | |
return ""+cursor.getString(3); | |
} | |
return null; | |
} catch (Exception e) { | |
Logcat.e("getPasswordFromPackageName ", "error : " + e.toString()); | |
return null; | |
} finally { | |
if (cursor != null && !cursor.isClosed()) { | |
cursor.close(); | |
} | |
} | |
} | |
public static boolean saveApplication(String appName, String packageName, | |
String password, String className, boolean isIndividualPassword) { | |
ApplicationInformation appInfo = isApplicationProtected(packageName); | |
if (appInfo != null) { | |
return true; | |
} | |
int isIndividualValue = 0; | |
if (isIndividualPassword) { | |
isIndividualValue = Globals.TRUE; | |
} else { | |
isIndividualValue = Globals.FALSE; | |
} | |
ContentValues values; | |
try { | |
values = new ContentValues(); | |
values.put(ApplicationInformation._applicationName, appName); | |
values.put(ApplicationInformation._packageName, packageName); | |
values.put(ApplicationInformation._password, password); | |
values.put(ApplicationInformation._className, className); | |
values.put(ApplicationInformation._individualSecure, | |
isIndividualValue); | |
db.insert(DBHelper.TABLE_APPLICATION, null, values); | |
return true; | |
} catch (Exception e) { | |
Logcat.e("TABLE_APPLICATION INSERT", "error : " + e.toString()); | |
return false; | |
} | |
} | |
public static boolean updateAppPassword(String packageName, | |
String password, boolean isCommonPassword) { | |
ContentValues values; | |
try { | |
values = new ContentValues(); | |
int isCommonValue = 0; | |
if (isCommonPassword) { | |
isCommonValue = Globals.TRUE; | |
} else { | |
isCommonValue = Globals.FALSE; | |
} | |
values.put(ApplicationInformation._password, password); | |
values.put(ApplicationInformation._individualSecure, isCommonValue); | |
db.update(DBHelper.TABLE_APPLICATION, values, "" | |
+ ApplicationInformation._packageName + " =?", | |
new String[] { String.valueOf(packageName) }); | |
Logcat.e("TABLE_APPLICATION Update", "Record Updated!"); | |
return true; | |
} catch (Exception e) { | |
Logcat.e("TABLE_APPLICATION Update", "error : " + e.toString()); | |
return false; | |
} | |
} | |
public static boolean deleteTemplate(String packageName) { | |
try { | |
db.execSQL("delete from " + DBHelper.TABLE_APPLICATION + " where " | |
+ ApplicationInformation._packageName + " = '" | |
+ packageName + "'"); | |
return true; | |
} catch (Exception e) { | |
Logcat.e("TABLE_APPLICATION deleted", "error : " + e.toString()); | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs2.applockfree.databases; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import com.vs2.applockfree.objects.ApplicationInformation; | |
import com.vs2.applockfree.utilities.Logcat; | |
public class DBHelper extends SQLiteOpenHelper { | |
public static final int DB_VERSION = 1; | |
public static final String DB_NAME = "applockDB"; | |
public static final String TABLE_APPLICATION = "appMaster"; | |
public static final String TAG = DBHelper.class.getSimpleName(); | |
@SuppressWarnings("unused") | |
private Context mContext; | |
public DBHelper(Context context) { | |
super(context, DB_NAME, null, DB_VERSION); | |
mContext = context; | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
// TODO Auto-generated method stub | |
String createTable; | |
createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_APPLICATION + "(" | |
+ ApplicationInformation._Id | |
+ " integer primary key autoincrement, " | |
+ ApplicationInformation._applicationName + " VARCHAR," | |
+ ApplicationInformation._packageName + " VARCHAR," | |
+ ApplicationInformation._password + " varchar, " | |
+ ApplicationInformation._className + " varchar, " | |
+ ApplicationInformation._individualSecure + " integer);"; | |
db.execSQL(createTable); | |
Logcat.e(TAG, "Database created successfully!"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// TODO Auto-generated method stub | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_APPLICATION); | |
this.onCreate(db); | |
Logcat.e(TAG, "Database updated successfully!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment