From: gumartinm Date: Sun, 10 Jun 2012 22:27:06 +0000 (+0200) Subject: Latest changes in Android mobiads application. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6583bb74fe915e9ffac8e24caf9d96587bb9141d;p=JavaForFun Latest changes in Android mobiads application. No time for comments. :( --- diff --git a/Android/MobiAds/gen/de/android/mobiads/R.java b/Android/MobiAds/gen/de/android/mobiads/R.java index a0ac68e..2928863 100644 --- a/Android/MobiAds/gen/de/android/mobiads/R.java +++ b/Android/MobiAds/gen/de/android/mobiads/R.java @@ -16,30 +16,34 @@ public final class R { public static final int wheelnotification=0x7f020002; } public static final class id { - public static final int ads_entry_icon=0x7f05000e; - public static final int ads_entry_text=0x7f050010; - public static final int ads_entry_title=0x7f05000f; - public static final int button_bind=0x7f050007; - public static final int button_lislocalads=0x7f05000d; - public static final int button_startservice=0x7f05000b; - public static final int button_stopservice=0x7f050009; - public static final int button_unbind=0x7f050008; - public static final int frameLayout1=0x7f050000; - public static final int frameLayout2=0x7f050003; - public static final int frameLayout3=0x7f050005; - public static final int frameLayout4=0x7f05000a; - public static final int frameLayout5=0x7f05000c; - public static final int list=0x7f050006; - public static final int login_button=0x7f050004; - public static final int password=0x7f050002; - public static final int username=0x7f050001; + public static final int ads_entry_icon=0x7f060000; + public static final int ads_entry_text=0x7f060002; + public static final int ads_entry_title=0x7f060001; + public static final int button_bind=0x7f06000a; + public static final int button_lislocalads=0x7f060010; + public static final int button_startservice=0x7f06000e; + public static final int button_stopservice=0x7f06000c; + public static final int button_unbind=0x7f06000b; + public static final int frameLayout1=0x7f060003; + public static final int frameLayout2=0x7f060006; + public static final int frameLayout3=0x7f060008; + public static final int frameLayout4=0x7f06000d; + public static final int frameLayout5=0x7f06000f; + public static final int list=0x7f060009; + public static final int login_button=0x7f060007; + public static final int menuadsremove=0x7f060011; + public static final int password=0x7f060005; + public static final int username=0x7f060004; } public static final class layout { - public static final int main=0x7f030000; - public static final int mobiadslist=0x7f030001; - public static final int mobiadsmain=0x7f030002; - public static final int mobiadsnewadslist=0x7f030003; - public static final int news_entry_list_item=0x7f030004; + public static final int ads_entry_list_item=0x7f030000; + public static final int main=0x7f030001; + public static final int mobiadslist=0x7f030002; + public static final int mobiadsmain=0x7f030003; + public static final int mobiadsnewadslist=0x7f030004; + } + public static final class menu { + public static final int menuads=0x7f050000; } public static final class string { public static final int alert_dialog_cancel=0x7f040004; @@ -59,6 +63,7 @@ public final class R { public static final int error_dialog_connection_error=0x7f040005; public static final int error_dialog_userpwd_error=0x7f040006; public static final int hello=0x7f040000; + public static final int menuads_remove=0x7f04001e; public static final int new_ads=0x7f040001; public static final int password=0x7f040008; public static final int remote_service_content_empty_notification=0x7f040015; diff --git a/Android/MobiAds/res/layout/ads_entry_list_item.xml b/Android/MobiAds/res/layout/ads_entry_list_item.xml new file mode 100644 index 0000000..13a01c3 --- /dev/null +++ b/Android/MobiAds/res/layout/ads_entry_list_item.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/MobiAds/res/layout/news_entry_list_item.xml b/Android/MobiAds/res/layout/news_entry_list_item.xml deleted file mode 100644 index 13a01c3..0000000 --- a/Android/MobiAds/res/layout/news_entry_list_item.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Android/MobiAds/res/menu/menuads.xml b/Android/MobiAds/res/menu/menuads.xml index 9467d9d..d1b7991 100644 --- a/Android/MobiAds/res/menu/menuads.xml +++ b/Android/MobiAds/res/menu/menuads.xml @@ -1,34 +1,12 @@ - - - - - - - - - - - + android:checkable="false" + android:visible="true" + android:enabled="true" /> \ No newline at end of file diff --git a/Android/MobiAds/res/values/strings.xml b/Android/MobiAds/res/values/strings.xml index d5ed866..123453f 100644 --- a/Android/MobiAds/res/values/strings.xml +++ b/Android/MobiAds/res/values/strings.xml @@ -31,4 +31,5 @@ MobieAds/1.0 UTF-8 description + Remove \ No newline at end of file diff --git a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java index 9befd15..064e0cc 100644 --- a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java @@ -108,6 +108,7 @@ public class MobiAdsMainActivity extends Activity { public void onClickListLocalAds(View v) { Intent intent = new Intent("android.intent.action.MOBIADSLIST"). setComponent(new ComponentName("de.android.mobiads", "de.android.mobiads.list.MobiAdsListActivity")); + intent.putExtra("login", false); this.startActivity(intent); } } diff --git a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java index 4237189..06ff45a 100644 --- a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java +++ b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java @@ -290,6 +290,7 @@ public class MobiAdsBatch { values.put(Indexer.Index.COLUMN_NAME_PATH, (String) objects.get("id")); values.put(Indexer.Index.COLUMN_NAME_TEXT, (String) objects.get("text")); values.put(Indexer.Index.COLUMN_NAME_URL, (String) objects.get("link")); + values.put(Indexer.Index.COLUMN_NAME_AD_NAME, (String) objects.get("adname")); values.put(Indexer.Index.COLUMN_NAME_IS_READ, new Integer(0)); //This method may throw SQLiteException (as a RunTimeException). So, without a try/catch block //there could be a leaked cursor... diff --git a/Android/MobiAds/src/de/android/mobiads/list/AdsEntry.java b/Android/MobiAds/src/de/android/mobiads/list/AdsEntry.java index 30cb3c3..f5492e2 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/AdsEntry.java +++ b/Android/MobiAds/src/de/android/mobiads/list/AdsEntry.java @@ -11,11 +11,15 @@ public final class AdsEntry { private final String title; private final String text; private final Bitmap icon; + private final int idAd; + private final String URL; - public AdsEntry(final String title, final String text, final Bitmap icon) { + public AdsEntry(final String title, final String text, final Bitmap icon, final int idAd, final String URL) { this.title = title; this.text = text; this.icon = icon; + this.idAd = idAd; + this.URL = URL; } /** @@ -39,4 +43,17 @@ public final class AdsEntry { return icon; } + /** + * @return Ad unique identifier of this ads entry + */ + public int getIdAd() { + return idAd; + } + + /** + * @return URL matching this ad. + */ + public String getURL() { + return URL; + } } diff --git a/Android/MobiAds/src/de/android/mobiads/list/AdsEntryAdapter.java b/Android/MobiAds/src/de/android/mobiads/list/AdsEntryAdapter.java index 26990ad..0d4dffb 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/AdsEntryAdapter.java +++ b/Android/MobiAds/src/de/android/mobiads/list/AdsEntryAdapter.java @@ -14,11 +14,11 @@ import android.widget.TextView; */ public final class AdsEntryAdapter extends ArrayAdapter { - private final int newsItemLayoutResource; + private final int adsItemLayoutResource; - public AdsEntryAdapter(final Context context, final int newsItemLayoutResource) { + public AdsEntryAdapter(final Context context, final int adsItemLayoutResource) { super(context, 0); - this.newsItemLayoutResource = newsItemLayoutResource; + this.adsItemLayoutResource = adsItemLayoutResource; } @Override @@ -51,7 +51,7 @@ public final class AdsEntryAdapter extends ArrayAdapter { final LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); - workingView = inflater.inflate(newsItemLayoutResource, null); + workingView = inflater.inflate(adsItemLayoutResource, null); } else { workingView = convertView; } diff --git a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java index 45a4300..2879512 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningServiceInfo; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -16,20 +18,26 @@ import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; +import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; +import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; +import de.android.mobiads.MobiAdsService; import de.android.mobiads.R; import de.android.mobiads.provider.Indexer; public class MobiAdsListActivity extends Activity { private static final String TAG = "MobiAdsListActivity"; + private AdsEntryAdapter newsEntryAdapter; + @Override public void onResume() { super.onResume(); @@ -37,7 +45,7 @@ public class MobiAdsListActivity extends Activity { // Setup the list view final ListView newsEntryListView = (ListView) findViewById(R.id.list); - final AdsEntryAdapter newsEntryAdapter = new AdsEntryAdapter(this, R.layout.news_entry_list_item); + newsEntryAdapter = new AdsEntryAdapter(this, R.layout.ads_entry_list_item); newsEntryListView.setAdapter(newsEntryAdapter); this.registerForContextMenu(newsEntryListView); @@ -51,7 +59,7 @@ public class MobiAdsListActivity extends Activity { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - Uri uri = Uri.parse(newsEntryAdapter.getItem(position).getTitle()); + Uri uri = Uri.parse(newsEntryAdapter.getItem(position).getURL()); startActivity(new Intent(Intent.ACTION_VIEW, uri)); } }); @@ -89,8 +97,10 @@ public class MobiAdsListActivity extends Activity { } } } - entries.add(new AdsEntry(cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_URL)), - cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_TEXT)), bitMap)); + entries.add(new AdsEntry(cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_AD_NAME)), + cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_TEXT)), bitMap, + cursor.getInt(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_ID_AD)), + cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_URL)))); if (cursor.getInt(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_IS_READ)) == 0) { values.put(Indexer.Index.COLUMN_NAME_IS_READ, new Integer(1)); @@ -104,7 +114,9 @@ public class MobiAdsListActivity extends Activity { cursor.close(); } - showNotification(0, 0, getText(R.string.remote_service_content_empty_notification)); + if (this.isMyServiceRunning()) { + showNotification(0, 0, getText(R.string.remote_service_content_empty_notification)); + } return entries; } @@ -116,6 +128,37 @@ public class MobiAdsListActivity extends Activity { inflater.inflate(R.menu.menuads, menu); } + @Override + public boolean onContextItemSelected(MenuItem item) { + AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + switch (item.getItemId()) { + case R.id.menuadsremove: + removeAd(info.position); + return true; + default: + return super.onContextItemSelected(item); + } + } + + public void removeAd(int position){ + AdsEntry entry = this.newsEntryAdapter.getItem(position); + int idAd = entry.getIdAd(); + Uri uriDelete = Uri.parse("content://" + "de.android.mobiads.provider" + "/" + "indexer" + "/idad/" + idAd); + + getContentResolver().delete(uriDelete, null, null); + this.newsEntryAdapter.remove(entry); + this.newsEntryAdapter.notifyDataSetChanged(); + } + + private boolean isMyServiceRunning() { + ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (MobiAdsService.class.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } public void showNotification(final int level, final int noReadAds, CharSequence contentText) { NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); diff --git a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java index 60f0910..fd42e16 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java @@ -5,12 +5,16 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; + +import de.android.mobiads.MobiAdsService; import de.android.mobiads.R; import de.android.mobiads.provider.Indexer; import android.app.Activity; +import android.app.ActivityManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.ActivityManager.RunningServiceInfo; import android.content.ContentValues; import android.content.Context; import android.content.Intent; @@ -34,7 +38,7 @@ public class MobiAdsNewAdsActivity extends Activity { // Setup the list view final ListView newsEntryListView = (ListView) findViewById(R.id.list); - final AdsEntryAdapter newsEntryAdapter = new AdsEntryAdapter(this, R.layout.news_entry_list_item); + final AdsEntryAdapter newsEntryAdapter = new AdsEntryAdapter(this, R.layout.ads_entry_list_item); newsEntryListView.setAdapter(newsEntryAdapter); // Populate the list, through the adapter. Should I populate the whole list right now? I do not think so... @@ -86,8 +90,10 @@ public class MobiAdsNewAdsActivity extends Activity { } } } - entries.add(new AdsEntry(cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_URL)), - cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_TEXT)), bitMap)); + entries.add(new AdsEntry(cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_AD_NAME)), + cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_TEXT)), bitMap, + cursor.getInt(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_ID_AD)), + cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_URL)))); cursor.getString(cursor.getColumnIndexOrThrow(Indexer.Index.COLUMN_NAME_PATH)); @@ -101,12 +107,23 @@ public class MobiAdsNewAdsActivity extends Activity { cursor.close(); } - - showNotification(0, 0, getText(R.string.remote_service_content_empty_notification)); + if (this.isMyServiceRunning()) { + showNotification(0, 0, getText(R.string.remote_service_content_empty_notification)); + } return entries; } + private boolean isMyServiceRunning() { + ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (MobiAdsService.class.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } + public void showNotification(final int level, final int noReadAds, CharSequence contentText) { NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, MobiAdsNewAdsActivity.class); diff --git a/Android/MobiAds/src/de/android/mobiads/provider/Indexer.java b/Android/MobiAds/src/de/android/mobiads/provider/Indexer.java index 5e3e866..c44756c 100644 --- a/Android/MobiAds/src/de/android/mobiads/provider/Indexer.java +++ b/Android/MobiAds/src/de/android/mobiads/provider/Indexer.java @@ -55,6 +55,13 @@ public final class Indexer { public static final String COLUMN_NAME_IS_READ = "isRead"; /** + * Column name for the Sqlite3 integer as boolean field.
+ * It let us know if the ad was already read by the user or not. + *

Type: TEXT

+ */ + public static final String COLUMN_NAME_AD_NAME = "adName"; + + /** * The default sort order for this table */ public static final String DEFAULT_SORT_ORDER = Index._ID; diff --git a/Android/MobiAds/src/de/android/mobiads/provider/IndexerOpenHelper.java b/Android/MobiAds/src/de/android/mobiads/provider/IndexerOpenHelper.java index aa0d270..bb88fc3 100644 --- a/Android/MobiAds/src/de/android/mobiads/provider/IndexerOpenHelper.java +++ b/Android/MobiAds/src/de/android/mobiads/provider/IndexerOpenHelper.java @@ -29,9 +29,10 @@ public class IndexerOpenHelper extends SQLiteOpenHelper { + Indexer.Index._ID + " INTEGER PRIMARY KEY, " + Indexer.Index.COLUMN_NAME_ID_AD + " INTEGER" + " UNIQUE" + " NOT NULL, " + Indexer.Index.COLUMN_NAME_PATH + " TEXT(15)" + " UNIQUE" + " NOT NULL, " - + Indexer.Index.COLUMN_NAME_URL + " TEXT(2000)" + " NOT NULL, " - + Indexer.Index.COLUMN_NAME_TEXT + " TEXT(255)" + " NOT NULL, " - + Indexer.Index.COLUMN_NAME_IS_READ + " INTEGER" + " NOT NULL " + + Indexer.Index.COLUMN_NAME_URL + " TEXT(3000)" + " NOT NULL, " + + Indexer.Index.COLUMN_NAME_TEXT + " TEXT(500)" + " NOT NULL, " + + Indexer.Index.COLUMN_NAME_IS_READ + " INTEGER" + " NOT NULL, " + + Indexer.Index.COLUMN_NAME_AD_NAME + " TEXT(255)" + " NOT NULL " + ");"); } diff --git a/Android/MobiAds/src/de/android/mobiads/provider/IndexerProvider.java b/Android/MobiAds/src/de/android/mobiads/provider/IndexerProvider.java index caebab5..fc0387d 100644 --- a/Android/MobiAds/src/de/android/mobiads/provider/IndexerProvider.java +++ b/Android/MobiAds/src/de/android/mobiads/provider/IndexerProvider.java @@ -80,6 +80,8 @@ public class IndexerProvider extends ContentProvider { sIndexerProjectionMap.put(Indexer.Index.COLUMN_NAME_URL, Indexer.Index.COLUMN_NAME_URL); sIndexerProjectionMap.put(Indexer.Index.COLUMN_NAME_IS_READ, Indexer.Index.COLUMN_NAME_IS_READ); + + sIndexerProjectionMap.put(Indexer.Index.COLUMN_NAME_AD_NAME, Indexer.Index.COLUMN_NAME_AD_NAME); } @@ -158,7 +160,21 @@ public class IndexerProvider extends ContentProvider { selectionArgs // The incoming where clause values. ); break; - + + case INDEXER_IDAD: + finalWhere = Indexer.Index.COLUMN_NAME_ID_AD + // the name of the ID column + "=" + + // the position of the Advertisement ID itself in the incoming URI + uri.getPathSegments().get(2); + if (selection != null) { + finalWhere = finalWhere + " AND " + selection; + } + count = db.delete( + Indexer.Index.TABLE_NAME, // The database table name. + finalWhere, // The final WHERE clause + selectionArgs // The incoming where clause values. + ); + break; default: throw new IllegalArgumentException("Unknown URI " + uri); } @@ -223,8 +239,9 @@ public class IndexerProvider extends ContentProvider { (values.containsKey(Indexer.Index.COLUMN_NAME_ID_AD) == false) || (values.containsKey(Indexer.Index.COLUMN_NAME_TEXT) == false) || (values.containsKey(Indexer.Index.COLUMN_NAME_URL) == false) || + (values.containsKey(Indexer.Index.COLUMN_NAME_AD_NAME) == false) || (values.containsKey(Indexer.Index.COLUMN_NAME_IS_READ) == false)){ - throw new SQLException("Missed parameter. Failed to insert row into " + uri); + throw new SQLException("Missed parameter. Failed to insert row into " + uri); } // Opens the database object in "write" mode.