New concept. Prepare to rewrite code for news

This commit is contained in:
Vladislav Khorev 2015-05-30 19:12:32 +00:00
parent 04faa66341
commit e882bf6e05
27 changed files with 2916 additions and 765 deletions

View File

@ -0,0 +1,112 @@
package fishrungames.bashgid;
import java.util.ArrayList;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import fishrungames.networkutils.ImageManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class AlbumListFragment extends Fragment
{
ListAdapter listAdapter;
GridView gridView;
ArrayList<AlbumShortData> albumArr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
albumArr = MainActivity.getInstance().albumDataSource.GetAllAlbumShortData();
View rootView = inflater.inflate(R.layout.fragment_video_page, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridView);
listAdapter = new ListAdapter(getActivity());
gridView.setAdapter(listAdapter);
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
MainActivity.getInstance().OpenPhotoAlbumScreen(albumArr.get(position).name);
}
});
return rootView;
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
public ListAdapter(Context c) {
mContext = c;
}
public int getCount() {
return albumArr.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.video_page_list_element, null);
} else {
view = convertView;
}
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);
ImageManager.getInstance().ApplyImageToImageView(imageView, albumArr.get(position).firstImageUrl);
nameTextView.setText(albumArr.get(position).title);
return view;
}
}
}

View File

@ -1,21 +1,41 @@
package fishrungames.bashgid;
import java.util.ArrayList;
import java.util.Locale;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
import fishrungames.bashgid.core.ArticleManager;
import fishrungames.bashgid.core.ArticleManager.ArticleRecordData;
import fishrungames.bashgid.core.BookManager.BookRecord;
import fishrungames.bashgid.core.HtmlDownloadManager.TextFileRecord;
import fishrungames.bashgid.core.NewsDownloadTask;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.VideoManager.VideoRecord;
import fishrungames.bashgid.core.PhotoManager.PhotoRecordData;
import fishrungames.bashgid.core.VideoManager;
import fishrungames.bashgid.core.VideoManager.VideoRecordData;
import fishrungames.bashgid.core.db.AlbumDataSource;
import fishrungames.bashgid.core.db.ArticleDataSource;
import fishrungames.bashgid.core.db.BashgidSqliteHelper;
import fishrungames.bashgid.core.db.ChannelDataSource;
import fishrungames.bashgid.core.db.JournalDataSource;
import fishrungames.bashgid.core.db.NewsDataSource;
import fishrungames.bashgid.core.db.PhotoDataSource;
import fishrungames.bashgid.core.db.VideoDataSource;
import fishrungames.networkutils.ContextHolder;
import fishrungames.networkutils.ImageDownloadTask;
import fishrungames.networkutils.ImageManager;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
@ -32,22 +52,35 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
public static final String TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT = "TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT";
public static final String TAG_FROM_BOOKSFRAGMENT_TO_BOOKRECORDFRAGMENT = "TAG_FROM_BOOKSFRAGMENT_TO_BOOKRECORDFRAGMENT";
public static final String TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT = "TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT";
public static final String TAG_TO_WELCOMEFRAGMENT = "WelcomeFragment";
public static final String TAG_TO_SEARCHFRAGMENT = "SearchFragment";
public static final String TAG_TO_MAINFRAGMENT = "MainFragment";
public static final String TAG_TO_MAPFRAGMENT = "MapFragment";
public static final int NEWS_DOWNLOADER_STATE_UPDATE = 10;
public static final int NEWS_DOWNLOADER_STATE_FINISHED = 11;
MainPageFragment mainPageFragment = null;
NewsListFragment newsListFragment = null;
static MainActivity instance = null;
private NavigationDrawerFragment mNavigationDrawerFragment = null;
public BashgidSqliteHelper dbHelper = null;
public NewsDataSource newsDataSource = null;
public PhotoDataSource photoDataSource = null;
public AlbumDataSource albumDataSource = null;
public ArticleDataSource articleDataSource = null;
public VideoDataSource videoDataSource = null;
public ChannelDataSource channelDataSource = null;
public JournalDataSource journalDataSource = null;
public Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
@ -57,17 +90,127 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
setContentView(R.layout.activity_main);
SetupDrawer();
ImageManager.getInstance().alwaysUpdateImagesWithoutHash = false;
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new WelcomeFragment(), TAG_TO_WELCOMEFRAGMENT).commit();
instance = this;
ContextHolder.setContext(instance);
newsDataSource = new NewsDataSource(this);
dbHelper = new BashgidSqliteHelper(this);
newsDataSource = new NewsDataSource(this, dbHelper);
photoDataSource = new PhotoDataSource(this, dbHelper);
newsDataSource.open();
albumDataSource = new AlbumDataSource(this, dbHelper);
articleDataSource = new ArticleDataSource(this, dbHelper);
videoDataSource = new VideoDataSource(this, dbHelper);
channelDataSource = new ChannelDataSource(this, dbHelper);
journalDataSource = new JournalDataSource(this, dbHelper);
mHandler = new Handler(Looper.getMainLooper())
{
@Override
public void handleMessage(Message inputMessage)
{
switch (inputMessage.what)
{
case ImageDownloadTask.IMAGE_DOWNLOADER_STATE_UPDATE:
case ImageDownloadTask.IMAGE_DOWNLOADER_STATE_FINISHED:
//UpdateOnImageDownloaded();
break;
case NEWS_DOWNLOADER_STATE_UPDATE:
case NEWS_DOWNLOADER_STATE_FINISHED:
//UpdateOnNewsDownloaded();
break;
default:
super.handleMessage(inputMessage);
}
}
};
/*
photoDataSource.AddOrReplacePhoto(new PhotoRecordData(
"Vlad",
"Vlad's photo",
"http://pp.vk.me/c623226/v623226718/2af84/NeEYYIlnR-I.jpg",
"",
0,
0
));
photoDataSource.AddOrReplacePhoto(new PhotoRecordData(
"Lilia",
"Lilia photo",
"http://http://pp.vk.me/c622725/v622725384/3290f/hfnoyPHe5y8.jpg",
"",
0,
0
));
*/
albumDataSource.CreateNewAlbum(new AlbumRecordData("TEST", "Vk Photos", "Wow"));
AlbumManager.DownloadAndAddPhotoToAlbum(new PhotoRecordData(
"Lilia",
"Lilia photo",
"http://pp.vk.me/c622725/v622725384/3290f/hfnoyPHe5y8.jpg",
"",
0,
0
), "TEST");
AlbumManager.DownloadAndAddPhotoToAlbum(new PhotoRecordData(
"Vlad",
"Vlad's photo",
"http://pp.vk.me/c623226/v623226718/2af84/NeEYYIlnR-I.jpg",
"",
0,
0
), "TEST");
articleDataSource.CreateNewArticle(new ArticleRecordData(
"TESTART",
0,
"Photos",
"Wow 2 photos",
0,
0,
""));
articleDataSource.AddAlbumToArticle("TEST", "TESTART");
AlbumManager.AddNewVideoToChannel(new VideoRecordData(
VideoManager.VIDEO_TYPE_YOUTUBE,
"http://www.youtube.com/watch?v=dQw4w9WgXcQ",
"Rickroll",
"Mega Rickroll",
"http://img.youtube.com/vi/dQw4w9WgXcQ/0.jpg",
""
), "main");
AlbumManager.AddNewArticleToJournal(new ArticleRecordData(
"testArticle2",
ArticleManager.ARTICLE_TYPE_NORMAL,
"My article",
"Wow content lol",
0,
0,
"http://fishrungames.ru"
), "main");
articleDataSource.AddAlbumToArticle("TEST", "testArticle2");
articleDataSource.AddChannelToArticle("main", "testArticle2");
}
@ -76,6 +219,100 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
return instance;
}
public void DownloadImageArr(String [] imageUrlArr)
{
Bundle innerQuery = new Bundle();
innerQuery.putStringArray("imageUrlArr", imageUrlArr);
ImageDownloadTask task = new ImageDownloadTask();
task.mHandler = mHandler;
task.execute(innerQuery);
}
public void DownloadImageArr(String [] imageUrlArr, String [] imageHashArr)
{
Bundle innerQuery = new Bundle();
innerQuery.putStringArray("imageUrlArr", imageUrlArr);
innerQuery.putStringArray("imageHashArr", imageHashArr);
ImageDownloadTask task = new ImageDownloadTask();
task.mHandler = mHandler;
task.execute(innerQuery);
}
public void DownloadImage(String imageUrl)
{
this.DownloadImageArr(new String []{imageUrl});
}
public void DownloadImage(String imageUrl, String imageHash)
{
this.DownloadImageArr(new String []{imageUrl}, new String []{imageHash});
}
public void ClearAllFragmentLinks()
{
mainPageFragment = null;
newsListFragment = null;
}
public void StartDownloadImagesTask(ArrayList<String> imageUrlArr, ArrayList<String> imageHashArr)
{
ImageDownloadTask task = new ImageDownloadTask();
task.mHandler = mHandler;
Bundle query = new Bundle();
query.putStringArray("imageUrlArr", (String[]) imageUrlArr.toArray());
task.execute(query);
}
public void StartDownloadNewsTask()
{
NewsDownloadTask task = new NewsDownloadTask();
task.mHandler = mHandler;
task.execute();
}
public void UpdateOnImageDownloaded()
{
if (mainPageFragment != null)
{
mainPageFragment.RefreshHeader();
}
if (newsListFragment != null)
{
newsListFragment.RefreshNewsList();
}
}
public void UpdateOnNewsDownloaded()
{
if (mainPageFragment != null)
{
mainPageFragment.RefreshHeader();
}
if (newsListFragment != null)
{
newsListFragment.RefreshNewsList();
}
}
public void OnSelectEnglish(View view)
{
@ -126,22 +363,33 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
public void OpenMainScreen()
{
ClearAllFragmentLinks();
mainPageFragment = new MainPageFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MainPageFragment()).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mainPageFragment).commit();
mNavigationDrawerFragment.EnableDrawer();
//StartDownloadNewsTask();
}
public void OpenNewsScreen()
{
ClearAllFragmentLinks();
newsListFragment = new NewsListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NewsListFragment()).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsListFragment).commit();
mNavigationDrawerFragment.EnableDrawer();
//StartDownloadNewsTask();
}
public void OpenSettingsScreen()
{
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();
@ -150,56 +398,71 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
public void OpenSearchScreen()
{
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SearchFragment(), TAG_TO_SEARCHFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenSearchScreen(boolean restorePreviousSearch)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SearchFragment(restorePreviousSearch), TAG_TO_SEARCHFRAGMENT).commit();
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SearchFragment(restorePreviousSearch), TAG_TO_SEARCHFRAGMENT)
.commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenLinkContentScreen(TextFileRecord contentRecord)
{
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new LinkContentFragment(contentRecord), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenMapScreen()
{
//getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapHolderFragment(), TAG_TO_MAINFRAGMENT).commit();
ClearAllFragmentLinks();
// getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
// new MapHolderFragment(), TAG_TO_MAINFRAGMENT).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new OsmMapHolderFragment(), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenStreetViewScreen(double lat, double lon)
{
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new StreetViewHolderFragment(lat, lon), TAG_TO_MAPFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenPhotoAlbumListScreen()
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PhotoAlbumListFragment(), TAG_TO_MAINFRAGMENT).commit();
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AlbumListFragment(), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenPhotoAlbumScreen(AlbumManager.AlbumStruct album)
public void OpenPhotoAlbumScreen(String albumName)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PhotoAlbumFragment(album), TAG_TO_MAINFRAGMENT).commit();
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new PhotoAlbumFragment(albumName), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenNewsRecordScreen(NewsRecord newsRecord, String tag)
{
// Xperimental -- addToBackStack provoke error "Class not found". Need
@ -209,31 +472,36 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
// I added workaround:
ClearAllFragmentLinks();
NewsRecordFragment newsRecordFragment = new NewsRecordFragment(newsRecord);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, newsRecordFragment, tag).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenHtmlViewerScreen(String url)
{
ClearAllFragmentLinks();
HtmlViewerFragment htmlViewerFragment = new HtmlViewerFragment();
htmlViewerFragment.url = url;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, htmlViewerFragment, TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenBooksScreen()
{
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new BooksFragment(), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenBookRecordScreen(BookRecord bookRecord, String tag)
{
// Xperimental -- addToBackStack provoke error "Class not found". Need
@ -242,6 +510,7 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
// new NewsRecordFragment(newsRecord)).addToBackStack(null).commit();
// I added workaround:
ClearAllFragmentLinks();
BookRecordFragment bookRecordFragment = new BookRecordFragment(bookRecord);
@ -249,18 +518,20 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
mNavigationDrawerFragment.EnableDrawer();
}
//TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT
public void OpenVideoListScreen()
// TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT
public void OpenVideoListScreen(String channelName)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new VideoListFragment(), TAG_TO_MAINFRAGMENT).commit();
ClearAllFragmentLinks();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new VideoListFragment(channelName), TAG_TO_MAINFRAGMENT).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void OpenVideoRecordScreen(VideoRecord videoRecord, String tag)
public void OpenVideoRecordScreen(String videoUrl, String tag)
{
// Xperimental -- addToBackStack provoke error "Class not found". Need
// to resolve somehow!
@ -269,13 +540,23 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
// I added workaround:
VideoRecordFragment videoRecordFragment = new VideoRecordFragment(videoRecord);
ClearAllFragmentLinks();
VideoRecordFragment videoRecordFragment = new VideoRecordFragment(videoUrl);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, videoRecordFragment, tag).commit();
mNavigationDrawerFragment.EnableDrawer();
}
public void UpdateMainScreenIfPossible(Bundle result)
{
if (mainPageFragment != null)
{
mainPageFragment.RefreshHeader();
}
}
@Override
public void onAttachFragment(Fragment fragment)
{
@ -299,9 +580,9 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.global, menu);
inflater.inflate(R.menu.global, menu);
return super.onCreateOptionsMenu(menu);
}
@ -312,17 +593,17 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_search)
{
if (getSupportFragmentManager().findFragmentByTag(TAG_TO_SEARCHFRAGMENT) == null
&& getSupportFragmentManager().findFragmentByTag(TAG_TO_WELCOMEFRAGMENT) == null)
&& getSupportFragmentManager().findFragmentByTag(TAG_TO_WELCOMEFRAGMENT) == null)
{
OpenSearchScreen();
}
return true;
}
if (id == R.id.action_settings)
{
return true;
@ -336,28 +617,24 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
if (position == 1)
{
OpenMainScreen();
}
else if (position == 2)
} else if (position == 2)
{
OpenNewsScreen();
}
else if (position == 4)
} else if (position == 4)
{
OpenMapScreen();
}
else if (position == 5)
} else if (position == 5)
{
OpenVideoListScreen();
}
else if (position == 6)
OpenVideoListScreen("main");
} else if (position == 6)
{
OpenPhotoAlbumListScreen();
}
else if (position == 7)
//OpenPhotoAlbumScreen(albumDataSource.GetAlbumFullData("TEST"));
} else if (position == 7)
{
OpenBooksScreen();
}
else if (position == 9)
} else if (position == 9)
{
OpenSettingsScreen();
}
@ -380,53 +657,48 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_BOOKSFRAGMENT_TO_BOOKRECORDFRAGMENT) != null)
{
OpenBooksScreen();
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT) != null)
{
OpenVideoListScreen();
OpenVideoListScreen("main");
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_FROM_SEARCHFRAGMENT_TO_NEWSRECORDFRAGMENT) != null)
{
OpenSearchScreen(true);
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_TO_SEARCHFRAGMENT) != null)
{
OpenMainScreen();
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_TO_MAINFRAGMENT) != null)
{
OpenMainScreen();
return;
}
if (getSupportFragmentManager().findFragmentByTag(TAG_TO_MAPFRAGMENT) != null)
{
OpenMapScreen();
return;
}
super.onBackPressed();
@ -441,19 +713,14 @@ public class MainActivity extends ActionBarActivity implements NavigationDrawerF
@Override
protected void onResume()
{
newsDataSource.open();
super.onResume();
}
@Override
protected void onPause()
{
newsDataSource.close();
super.onPause();
}
}

View File

@ -3,16 +3,15 @@ package fishrungames.bashgid;
import java.util.ArrayList;
import fishrungames.networkutils.ImageManager;
import fishrungames.networkutils.UniversalActionWithCallback;
import fishrungames.networkutils.interfaces.RemoveCallbackInterface;
import fishrungames.networkutils.interfaces.UpdateAndFinishCallbackInterface;
import fishrungames.bashgid.core.MainPageUpdater;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
@ -25,7 +24,7 @@ import android.widget.ListView;
import android.widget.TextView;
import it.sephiroth.android.library.widget.HListView;
public class MainPageFragment extends Fragment implements UpdateAndFinishCallbackInterface {
public class MainPageFragment extends Fragment {
String [] teaserHeaderArray = { "Разное"};
@ -36,11 +35,6 @@ public class MainPageFragment extends Fragment implements UpdateAndFinishCallbac
ListView listView;
ListAdapter listAdapter;
View header;
//View newsView;
//View searchView;
RemoveCallbackInterface removeCallback = null;
Object removeCallbackSynchronizer = new Object();
@SuppressLint("InflateParams")
@Override
@ -50,22 +44,15 @@ public class MainPageFragment extends Fragment implements UpdateAndFinishCallbac
listView = (ListView) rootView.findViewById(R.id.listView);
listAdapter = new ListAdapter(getActivity());
listView.setAdapter(listAdapter);
//newsView = inflater.inflate(R.layout.main_page_news, null);
//searchView = inflater.inflate(R.layout.main_page_search, null);
header = inflater.inflate(R.layout.main_page_header, null);
RefreshHeader();
removeCallback = UniversalActionWithCallback.PerformActionIfNotPerforming(new MainPageUpdater(), null, this, "MainPageUpdater");
listView.addHeaderView(header);
//listView.addFooterView(header);
Button moreNewsButton = (Button) header.findViewById(R.id.moreNewsButton);
moreNewsButton.setOnClickListener(new OnClickListener()
@ -78,30 +65,12 @@ public class MainPageFragment extends Fragment implements UpdateAndFinishCallbac
}
}
);
return rootView;
}
@Override
public void onDestroyView()
{
synchronized (removeCallbackSynchronizer)
{
if (removeCallback != null)
{
removeCallback.RemoveCallback();
removeCallback = null;
}
}
super.onDestroyView();
}
private void RefreshHeader()
public void RefreshHeader()
{
@ -220,30 +189,6 @@ public class MainPageFragment extends Fragment implements UpdateAndFinishCallbac
}
@Override
public void OnFinished()
{
synchronized (removeCallbackSynchronizer)
{
removeCallback = null;
}
}
@Override
public void OnUpdated(Bundle result)
{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run()
{
RefreshHeader();
}
});
}
public static class NewsButtonOnClickListener implements OnClickListener
{

View File

@ -4,9 +4,6 @@ import java.util.ArrayList;
import fishrungames.networkutils.ImageManager;
import fishrungames.networkutils.UniversalActionWithCallback;
import fishrungames.networkutils.interfaces.RemoveCallbackInterface;
import fishrungames.networkutils.interfaces.UpdateAndFinishCallbackInterface;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.db.NewsDataSource;
@ -25,7 +22,7 @@ import android.widget.ListView;
import android.widget.TextView;
public class NewsListFragment extends Fragment implements UpdateAndFinishCallbackInterface
public class NewsListFragment extends Fragment
{
ListView listView;
@ -34,18 +31,12 @@ public class NewsListFragment extends Fragment implements UpdateAndFinishCallbac
ArrayList<NewsRecord> newsRecordArr;
RemoveCallbackInterface removeCallback = null;
Object removeCallbackSynchronizer = new Object();
@SuppressLint("InflateParams") @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news_page, container, false);
newsRecordArr = NewsManager.getInstance().getNews();
removeCallback = UniversalActionWithCallback.PerformActionIfNotPerforming(NewsManager.getInstance(), null, this, "NewsManager");
listView = (ListView) rootView.findViewById(R.id.listView);
listAdapter = new ListAdapter(getActivity());
@ -59,19 +50,6 @@ public class NewsListFragment extends Fragment implements UpdateAndFinishCallbac
return rootView;
}
@Override
public void onDestroyView()
{
synchronized (removeCallbackSynchronizer)
{
if (removeCallback != null)
{
removeCallback.RemoveCallback();
removeCallback = null;
}
}
super.onDestroyView();
}
public void RefreshNewsList()
{
@ -134,31 +112,7 @@ public class NewsListFragment extends Fragment implements UpdateAndFinishCallbac
}
@Override
public void OnFinished()
{
synchronized (removeCallbackSynchronizer)
{
removeCallback = null;
}
}
@Override
public void OnUpdated(Bundle result)
{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run()
{
RefreshNewsList();
}
});
}
public static class ButtonOnClickListener implements OnClickListener
{

View File

@ -15,23 +15,44 @@ import android.widget.ImageView;
import android.widget.ListView;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.networkutils.ImageManager;
public class PhotoAlbumFragment extends Fragment
{
AlbumManager.AlbumStruct album;
AlbumFullData album;
String albumName;
ListAdapter listAdapter;
public PhotoAlbumFragment(AlbumManager.AlbumStruct album)
public PhotoAlbumFragment()
{
this.album = album;
album = null;
albumName = "";
}
public PhotoAlbumFragment(String albumName)
{
this.albumName = albumName;
RestoreAlbum();
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("albumName", albumName);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
albumName = savedInstanceState.getString("albumName");
RestoreAlbum();
}
View rootView = inflater.inflate(R.layout.fragment_photo_album_page, container, false);
listAdapter = new ListAdapter(getActivity());
@ -43,6 +64,11 @@ public class PhotoAlbumFragment extends Fragment
return rootView;
}
void RestoreAlbum()
{
album = MainActivity.getInstance().albumDataSource.GetAlbumFullData(albumName);
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
@ -51,7 +77,7 @@ public class PhotoAlbumFragment extends Fragment
}
public int getCount() {
return album.imageIds.size();
return album.photoRecordArr.size();
}
public Object getItem(int position) {
@ -76,11 +102,10 @@ public class PhotoAlbumFragment extends Fragment
} else {
view = convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
ImageManager.getInstance().ApplyImageToImageView(imageView, album.imageIds.get(position));
ImageManager.getInstance().ApplyImageToImageView(imageView, album.photoRecordArr.get(position).imageUrl);
return view;
}

View File

@ -1,115 +0,0 @@
package fishrungames.bashgid;
import java.util.ArrayList;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.networkutils.ImageManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
public class PhotoAlbumListFragment extends Fragment
{
ArrayList<AlbumManager.AlbumStruct> albumArr;
ListAdapter listAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photo_album_list_page, container, false);
albumArr = AlbumManager.getInstance().getAlbums();
listAdapter = new ListAdapter(getActivity());
ListView listView = (ListView) rootView.findViewById(R.id.listView);
listView.setAdapter(listAdapter);
return rootView;
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
public ListAdapter(Context c) {
mContext = c;
}
public int getCount() {
return albumArr.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.photo_album_list_element, null);
} else {
view = convertView;
}
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
ImageManager.getInstance().ApplyImageToImageView(imageButton, albumArr.get(position).imageIds.get(0));
imageButton.setOnClickListener(new ButtonOnClickListener(albumArr.get(position)));
TextView textView = (TextView)view.findViewById(R.id.textView);
textView.setText(albumArr.get(position).name);
return view;
}
}
public static class ButtonOnClickListener implements OnClickListener
{
AlbumManager.AlbumStruct album;
public ButtonOnClickListener(AlbumManager.AlbumStruct album)
{
this.album = album;
}
@Override
public void onClick(View v)
{
MainActivity.getInstance().OpenPhotoAlbumScreen(album);
}
}
}

View File

@ -1,14 +1,10 @@
package fishrungames.bashgid;
import java.util.ArrayList;
import fishrungames.bashgid.core.VideoManager;
import fishrungames.bashgid.core.VideoManager.VideoRecord;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
import fishrungames.networkutils.ImageManager;
import fishrungames.networkutils.UniversalActionWithCallback;
import fishrungames.networkutils.interfaces.RemoveCallbackInterface;
import fishrungames.networkutils.interfaces.UpdateAndFinishCallbackInterface;
import android.annotation.SuppressLint;
import android.content.Context;
@ -26,27 +22,42 @@ import android.widget.ImageView;
import android.widget.TextView;
public class VideoListFragment extends Fragment implements UpdateAndFinishCallbackInterface
public class VideoListFragment extends Fragment
{
ListAdapter listAdapter;
GridView gridView;
ArrayList<VideoRecord> videoRecordArr;
RemoveCallbackInterface removeCallback = null;
Object removeCallbackSynchronizer = new Object();
String channelName;
ChannelFullData channelFullData;
public VideoListFragment()
{
channelName = "";
channelFullData = null;
}
public VideoListFragment(String channelName)
{
this.channelName = channelName;
RestoreChannel();
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("channelName", channelName);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
videoRecordArr = VideoManager.getInstance().getVideoList();
removeCallback = UniversalActionWithCallback.PerformActionIfNotPerforming(VideoManager.getInstance(), null, this, "VideoManager");
if (savedInstanceState != null) {
channelName = savedInstanceState.getString("channelName");
RestoreChannel();
}
View rootView = inflater.inflate(R.layout.fragment_video_page, container, false);
@ -62,61 +73,20 @@ public class VideoListFragment extends Fragment implements UpdateAndFinishCallba
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
MainActivity.getInstance().OpenVideoRecordScreen(videoRecordArr.get(position), MainActivity.TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT);
MainActivity.getInstance().OpenVideoRecordScreen(channelFullData.videoRecordArr.get(position).videoUrl, MainActivity.TAG_FROM_VIDEOLISTFRAGMENT_TO_VIDEORECORDFRAGMENT);
}
});
RefreshVideoList();
return rootView;
}
@Override
public void onDestroyView()
void RestoreChannel()
{
synchronized (removeCallbackSynchronizer)
{
if (removeCallback != null)
{
removeCallback.RemoveCallback();
removeCallback = null;
}
}
super.onDestroyView();
channelFullData = MainActivity.getInstance().channelDataSource.GetChannelFullData(channelName);
}
public void RefreshVideoList()
{
videoRecordArr = VideoManager.getInstance().getVideoList();
listAdapter.notifyDataSetChanged();
}
@Override
public void OnFinished()
{
synchronized (removeCallbackSynchronizer)
{
removeCallback = null;
}
}
@Override
public void OnUpdated(Bundle result)
{
getActivity().runOnUiThread(new Runnable() {
@Override
public void run()
{
RefreshVideoList();
}
});
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
@ -125,7 +95,7 @@ public class VideoListFragment extends Fragment implements UpdateAndFinishCallba
}
public int getCount() {
return videoRecordArr.size();
return channelFullData.videoRecordArr.size();
}
public Object getItem(int position) {
@ -154,9 +124,9 @@ public class VideoListFragment extends Fragment implements UpdateAndFinishCallba
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);
ImageManager.getInstance().ApplyImageToImageView(imageView, videoRecordArr.get(position).imageUrl);
ImageManager.getInstance().ApplyImageToImageView(imageView, channelFullData.videoRecordArr.get(position).previewImageUrl);
nameTextView.setText(videoRecordArr.get(position).name);
nameTextView.setText(channelFullData.videoRecordArr.get(position).title);
return view;
}

View File

@ -1,8 +1,10 @@
package fishrungames.bashgid;
import fishrungames.bashgid.core.VideoManager;
import fishrungames.bashgid.core.VideoManager.VideoRecordData;
import fishrungames.networkutils.ImageManager;
import fishrungames.bashgid.core.VideoManager.VideoRecord;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
@ -18,11 +20,20 @@ import android.widget.TextView;
public class VideoRecordFragment extends Fragment
{
VideoRecord videoRecord;
String videoUrl;
public VideoRecordFragment(VideoRecord videoRecord)
VideoRecordData recordData;
public VideoRecordFragment()
{
this.videoRecord = videoRecord;
this.videoUrl = "";
this.recordData = null;
}
public VideoRecordFragment(String videoUrl)
{
this.videoUrl = videoUrl;
RestoreVideo();
}
@Override
@ -30,13 +41,13 @@ public class VideoRecordFragment extends Fragment
View rootView = inflater.inflate(R.layout.fragment_video_record_page, container, false);
TextView nameTextView = (TextView) rootView.findViewById(R.id.nameTextView);
nameTextView.setText(videoRecord.name);
nameTextView.setText(recordData.title);
TextView descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);
descriptionTextView.setText(videoRecord.description);
descriptionTextView.setText(recordData.description);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
ImageManager.getInstance().ApplyImageToImageView(imageView, videoRecord.imageUrl);
ImageManager.getInstance().ApplyImageToImageView(imageView, recordData.previewImageUrl);
imageView.setOnClickListener(new OnClickListener()
{
@ -44,9 +55,9 @@ public class VideoRecordFragment extends Fragment
@Override
public void onClick(View v)
{
if (videoRecord.videoType.equals("youtube"))
if (recordData.type == VideoManager.VIDEO_TYPE_YOUTUBE)
{
MainActivity.getInstance().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoRecord.videoUrl)));
MainActivity.getInstance().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(recordData.videoUrl)));
}
}
@ -56,4 +67,9 @@ public class VideoRecordFragment extends Fragment
}
void RestoreVideo()
{
recordData = MainActivity.getInstance().videoDataSource.GetVideoByVideoUrl(videoUrl);
}
}

View File

@ -2,74 +2,155 @@ package fishrungames.bashgid.core;
import java.util.ArrayList;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.ArticleManager.ArticleRecordData;
import fishrungames.bashgid.core.ChannelManager.ChannelRecordData;
import fishrungames.bashgid.core.JournalManager.JournalRecordData;
import fishrungames.bashgid.core.PhotoManager.PhotoRecordData;
import fishrungames.bashgid.core.VideoManager.VideoRecordData;
public class AlbumManager
{
public static class AlbumStruct
public static class AlbumRecordData
{
public String name;
public ArrayList<String> imageIds = new ArrayList<String>();
public String title;
public String description;
public AlbumStruct(String name)
public AlbumRecordData(String name, String title, String description)
{
this.name = name;
}
}
private ArrayList<AlbumStruct> AlbumArr = new ArrayList<AlbumStruct>();
public static AlbumManager instance = null;
public static AlbumManager getInstance()
{
if (instance == null)
{
instance = new AlbumManager();
this.title = title;
this.description = description;
}
return instance;
}
public void AddPhotoToAlbum(String albumName, String imageId)
{
synchronized (AlbumArr)
public AlbumRecordData(AlbumRecordData copyFrom)
{
boolean albumExists = false;
for (AlbumStruct albumStruct : AlbumArr)
{
if (albumStruct.name.equals(albumName))
{
albumStruct.imageIds.add(imageId);
albumExists = true;
}
}
if (!albumExists)
{
AlbumStruct albumStruct = new AlbumStruct(albumName);
albumStruct.imageIds.add(imageId);
AlbumArr.add(albumStruct);
}
this.name = copyFrom.name;
this.title = copyFrom.title;
this.description = copyFrom.description;
}
}
public ArrayList<AlbumStruct> getAlbums()
public static class AlbumFullData extends AlbumRecordData
{
ArrayList<AlbumStruct> albums = new ArrayList<AlbumStruct>();
public ArrayList<PhotoManager.PhotoRecordData> photoRecordArr;
synchronized(AlbumArr)
public AlbumFullData(AlbumRecordData recordData)
{
albums.addAll(AlbumArr);
super(recordData);
this.photoRecordArr = new ArrayList<PhotoManager.PhotoRecordData>();
}
}
public static class AlbumShortData extends AlbumRecordData
{
public String firstImageUrl;
public int photoCount;
public AlbumShortData(AlbumRecordData recordData)
{
super(recordData);
firstImageUrl = "";
photoCount = 0;
}
}
public static class AlbumRecord extends AlbumRecordData
{
public int id;
public AlbumRecord(int id, String name, String title, String description)
{
super(name, title, description);
this.id = id;
}
return albums;
}
public static void DownloadAndAddPhoto(PhotoRecordData recordData)
{
MainActivity.getInstance().DownloadImage(recordData.imageUrl, recordData.imageHash);
MainActivity.getInstance().photoDataSource.AddOrReplacePhoto(recordData);
}
public static void AddExistingPhotoToAlbum(String imageUrl, String albumName)
{
if (!MainActivity.getInstance().albumDataSource.IsAlbumAlreadyExist(albumName))
{
MainActivity.getInstance().albumDataSource.CreateNewAlbum(new AlbumRecordData(albumName, albumName, ""));
}
MainActivity.getInstance().albumDataSource.AddPhotoToAlbum(imageUrl, albumName);
}
public static void DownloadAndAddPhotoToAlbum(PhotoRecordData recordData, String albumName)
{
DownloadAndAddPhoto(recordData);
AddExistingPhotoToAlbum(recordData.imageUrl, albumName);
}
public static void AddNewVideo(VideoRecordData recordData)
{
MainActivity.getInstance().DownloadImage(recordData.previewImageUrl, recordData.previewImageHash);
MainActivity.getInstance().videoDataSource.AddOrReplaceVideo(recordData);
}
public static void AddExistingVideoToChannel(String videoUrl, String channelName)
{
if (!MainActivity.getInstance().channelDataSource.IsChannelAlreadyExist(channelName))
{
MainActivity.getInstance().channelDataSource.CreateNewChannel(new ChannelRecordData(channelName, channelName, ""));
}
MainActivity.getInstance().channelDataSource.AddVideoToChannel(videoUrl, channelName);
}
public static void AddNewVideoToChannel(VideoRecordData recordData, String channelName)
{
AddNewVideo(recordData);
AddExistingVideoToChannel(recordData.videoUrl, channelName);
}
public static void AddNewArticle(ArticleRecordData recordData)
{
MainActivity.getInstance().articleDataSource.CreateNewArticle(recordData);
}
public static void AddExistingArticleToJournal(String articleName, String journalName)
{
if (!MainActivity.getInstance().journalDataSource.IsJournalAlreadyExist(journalName))
{
MainActivity.getInstance().journalDataSource.CreateNewJournal(new JournalRecordData(journalName, journalName, ""));
}
MainActivity.getInstance().journalDataSource.AddArticleToJournal(articleName, journalName);
}
public static void AddNewArticleToJournal(ArticleRecordData recordData, String journalName)
{
AddNewArticle(recordData);
AddExistingArticleToJournal(recordData.name, journalName);
}
}

View File

@ -0,0 +1,83 @@
package fishrungames.bashgid.core;
import java.util.ArrayList;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
public class ArticleManager
{
public static final int ARTICLE_TYPE_NORMAL = 0;
public static class ArticleRecordData
{
public String name;
public int type;
public String title;
public String content;
public double geoLat;
public double geoLon;
public String externalLink;
public ArticleRecordData(String name, int type, String title, String content, double geoLat, double geoLon, String externalLink)
{
this.name = name;
this.type = type;
this.title = title;
this.content = content;
this.geoLat = geoLat;
this.geoLon = geoLon;
this.externalLink = externalLink;
}
public ArticleRecordData(ArticleRecordData copyFrom)
{
this.name = copyFrom.name;
this.type = copyFrom.type;
this.title = copyFrom.title;
this.content = copyFrom.content;
this.geoLat = copyFrom.geoLat;
this.geoLon = copyFrom.geoLon;
this.externalLink = copyFrom.externalLink;
}
}
public static class ArticleRecord extends ArticleRecordData
{
public int id;
public ArticleRecord(int id, ArticleRecordData recordData)
{
super(recordData);
this.id = id;
}
}
public static class ArticleFullData extends ArticleRecordData
{
public ArrayList<AlbumFullData> albumArr;
public ArrayList<ChannelFullData> channelArr;
public ArticleFullData(ArticleRecordData recordData)
{
super(recordData);
albumArr = new ArrayList<AlbumFullData>();
channelArr = new ArrayList<ChannelFullData>();
}
}
public static class ArticleShortData extends ArticleRecordData
{
public String previewImageUrl;
public ArticleShortData(ArticleRecordData recordData)
{
super(recordData);
previewImageUrl = "";
//albumArr = new ArrayList<AlbumFullData>();
//channelArr = new ArrayList<ChannelFullData>();
}
}
}

View File

@ -74,6 +74,7 @@ public class BookManager implements NetworkActionInterface
public void InThreadAction(Bundle query, UpdateCallbackHolder callbackHolder)
{
/*
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < BookArr.size(); i++)
@ -88,5 +89,6 @@ public class BookManager implements NetworkActionInterface
ImageManager.getInstance().InThreadAction(innerQuery, callbackHolder);
callbackHolder.OnUpdated(null);
*/
}
}

View File

@ -0,0 +1,88 @@
package fishrungames.bashgid.core;
import java.util.ArrayList;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.PhotoManager.PhotoRecordData;
public class ChannelManager
{
public static class ChannelRecordData
{
public String name;
public String title;
public String description;
public ChannelRecordData(String name, String title, String description)
{
this.name = name;
this.title = title;
this.description = description;
}
public ChannelRecordData(ChannelRecordData copyFrom)
{
this.name = copyFrom.name;
this.title = copyFrom.title;
this.description = copyFrom.description;
}
}
public static class ChannelFullData extends ChannelRecordData
{
public ArrayList<VideoManager.VideoRecordData> videoRecordArr;
public ChannelFullData(ChannelRecordData recordData)
{
super(recordData);
this.videoRecordArr = new ArrayList<VideoManager.VideoRecordData>();
}
}
public static class ChannelRecord extends ChannelRecordData
{
public int id;
public ChannelRecord(int id, String name, String title, String description)
{
super(name, title, description);
this.id = id;
}
}
/*
public static void DownloadAndAddPhoto(PhotoRecordData recordData)
{
MainActivity.getInstance().DownloadImage(recordData.imageUrl, recordData.imageHash);
MainActivity.getInstance().photoDataSource.AddOrReplacePhoto(recordData);
}
public static void AddExistingPhotoToAlbum(String imageUrl, String albumName)
{
if (!MainActivity.getInstance().albumDataSource.IsAlbumAlreadyExist(albumName))
{
MainActivity.getInstance().albumDataSource.CreateNewAlbum(new AlbumRecordData(albumName, albumName, ""));
}
MainActivity.getInstance().albumDataSource.AddPhotoToAlbum(imageUrl, albumName);
}
public static void DownloadAndAddPhotoToAlbum(PhotoRecordData recordData, String albumName)
{
DownloadAndAddPhoto(recordData);
AddExistingPhotoToAlbum(recordData.imageUrl, albumName);
}
*/
}

View File

@ -229,7 +229,8 @@ public class HtmlDownloadManager implements NetworkActionInterface
if (!imageLink.equals(""))
{
AlbumManager.getInstance().AddPhotoToAlbum(htmlUrlList.get(i).title, imageLink);
//Xperimental
//AlbumManager.getInstance().AddPhotoToAlbum(htmlUrlList.get(i).title, "", imageLink);
htmlUrlList.get(i).contentArr.add(new TextFileRecord.DocPart(TextFileRecord.DocPart.DocPartType.DPT_IMAGE, imageLink));
}
}

View File

@ -0,0 +1,45 @@
package fishrungames.bashgid.core;
public class JournalManager
{
public static class JournalRecordData
{
public String name;
public String title;
public String description;
public JournalRecordData(String name, String title, String description)
{
this.name = name;
this.title = title;
this.description = description;
}
public JournalRecordData(JournalRecordData copyFrom)
{
this.name = copyFrom.name;
this.title = copyFrom.title;
this.description = copyFrom.description;
}
}
//public static class Jo
public static class JournalRecord extends JournalRecordData
{
public int id;
public JournalRecord(int id, String name, String title, String description)
{
super(name, title, description);
this.id = id;
}
}
}

View File

@ -1,20 +0,0 @@
package fishrungames.bashgid.core;
import android.os.Bundle;
import fishrungames.networkutils.UpdateCallbackHolder;
import fishrungames.networkutils.interfaces.NetworkActionInterface;
public class MainPageUpdater implements NetworkActionInterface
{
@Override
public void InThreadAction(Bundle query, UpdateCallbackHolder callbackHolder)
{
NewsManager.getInstance().InThreadAction(query, callbackHolder);
HtmlDownloadManager.getInstance().InThreadAction(query, callbackHolder);
callbackHolder.OnUpdated(null);
}
}

View File

@ -0,0 +1,66 @@
package fishrungames.bashgid.core;
import java.util.ArrayList;
import java.util.Collections;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsManager.NewsSortComparator;
import fishrungames.networkutils.ImageDownloadTask;
public class NewsDownloadTask extends AsyncTask<Bundle, Integer, Long>
{
public Handler mHandler;
protected Long doInBackground(Bundle... queryArr)
{
ArrayList<NewsRecord> localNewsRecordArr = MainActivity.getInstance().newsDataSource.getNews();
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < NewsManager.urlArr.length; i++)
{
NewsManager.LoadNewsAndImagesFromRss2(NewsManager.urlArr[i], localNewsRecordArr, imageToDownloadList);
}
Collections.sort(localNewsRecordArr, new NewsSortComparator());
NewsManager.getInstance().replaceNews(localNewsRecordArr);
Bundle innerQuery = new Bundle();
innerQuery.putStringArray("imageUrlArr", imageToDownloadList.toArray(new String[imageToDownloadList.size()]));
ImageDownloadTask task = new ImageDownloadTask();
task.mHandler = this.mHandler;
task.execute(innerQuery);
return (long) 0;
}
protected void onProgressUpdate(Integer... progress)
{
Message completeMessage = mHandler.obtainMessage(MainActivity.NEWS_DOWNLOADER_STATE_UPDATE, this);
completeMessage.sendToTarget();
}
protected void onPostExecute(Long result)
{
Message completeMessage = mHandler.obtainMessage(MainActivity.NEWS_DOWNLOADER_STATE_FINISHED, this);
completeMessage.sendToTarget();
}
}

View File

@ -1,27 +1,24 @@
package fishrungames.bashgid.core;
import fishrungames.networkutils.ImageManager;
import fishrungames.networkutils.UpdateCallbackHolder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.concurrent.Semaphore;
import fishrungames.networkutils.DownloadFunctions;
import fishrungames.networkutils.interfaces.NetworkActionInterface;
import android.os.Bundle;
import android.util.Log;
import fishrungames.bashgid.MainActivity;
public class NewsManager implements NetworkActionInterface {
public class NewsManager {
private static final String [] urlArr = {
public static final String [] urlArr = {
"https://www.bashkortostan.ru/presscenter/news/rss/",
"http://www.bashedu.ru/rss.xml",
"http://www.bashinform.ru/rss/all.xml",
@ -93,40 +90,7 @@ public class NewsManager implements NetworkActionInterface {
}
}
@Override
public void InThreadAction(Bundle query, UpdateCallbackHolder callbackHolder)
{
ArrayList<NewsRecord> localNewsRecord = MainActivity.getInstance().newsDataSource.getNews();
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < urlArr.length; i++)
{
LoadNewsAndImagesFromRss2(urlArr[i], localNewsRecord, imageToDownloadList);
}
Collections.sort(localNewsRecord, new NewsSortComparator());
synchronized(newsRecord)
{
newsRecord.clear();
newsRecord.addAll(localNewsRecord);
MainActivity.getInstance().newsDataSource.replaceNews(newsRecord);
}
Bundle innerQuery = new Bundle();
innerQuery.putStringArray("imageUrlArr", imageToDownloadList.toArray(new String[imageToDownloadList.size()]));
ImageManager.getInstance().InThreadAction(innerQuery, callbackHolder);
callbackHolder.OnUpdated(null);
}
public NewsManager()
{
synchronized(newsRecord)
@ -135,6 +99,16 @@ public class NewsManager implements NetworkActionInterface {
}
}
public void replaceNews(ArrayList<NewsRecord> freshNews)
{
synchronized(newsRecord)
{
newsRecord.clear();
newsRecord.addAll(freshNews);
MainActivity.getInstance().newsDataSource.replaceNews(newsRecord);
}
}
public ArrayList<NewsRecord> getNews()
{

View File

@ -0,0 +1,63 @@
package fishrungames.bashgid.core;
public class PhotoManager
{
public static class PhotoRecordData
{
public String title;
public String description;
public String imageUrl;
public String imageHash;
public double geoLat;
public double geoLon;
public PhotoRecordData(String title, String description, String imageUrl, String imageHash, double geoLat, double geoLon)
{
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.imageHash = imageHash;
this.geoLat = geoLat;
this.geoLon = geoLon;
}
public PhotoRecordData(PhotoRecordData copyFrom)
{
this.title = copyFrom.title;
this.description = copyFrom.description;
this.imageUrl = copyFrom.imageUrl;
this.imageHash = copyFrom.imageHash;
this.geoLat = copyFrom.geoLat;
this.geoLon = copyFrom.geoLon;
}
}
public static class PhotoRecord extends PhotoRecordData
{
public int id;
PhotoRecord(int id, PhotoRecordData data)
{
super(data);
this.id = id;
}
}
static PhotoManager instance = null;
public static PhotoManager getInstance()
{
if (instance == null)
{
instance = new PhotoManager();
}
return instance;
}
}

View File

@ -1,95 +1,55 @@
package fishrungames.bashgid.core;
import java.util.ArrayList;
import android.os.Bundle;
import fishrungames.networkutils.ImageManager;
import fishrungames.networkutils.UpdateCallbackHolder;
import fishrungames.networkutils.interfaces.NetworkActionInterface;
public class VideoManager implements NetworkActionInterface
public class VideoManager
{
public static final int VIDEO_TYPE_YOUTUBE = 1;
public static final int VIDEO_TYPE_OTHER = 2;
public static class VideoRecord
public static class VideoRecordData
{
public String name;
public int type;
public String videoUrl;
public String imageUrl;
public String videoType;
public String title;
public String description;
public String previewImageUrl;
public String previewImageHash;
public VideoRecord(String name, String videoUrl, String imageUrl, String videoType, String description)
public VideoRecordData(int type, String videoUrl, String title, String description, String previewImageUrl, String previewImageHash)
{
this.name = name;
this.type = type;
this.videoUrl = videoUrl;
this.imageUrl = imageUrl;
this.videoType = videoType;
this.title = title;
this.description = description;
this.previewImageUrl = previewImageUrl;
this.previewImageHash = previewImageHash;
}
}
private ArrayList<VideoRecord> VideoArr = new ArrayList<VideoRecord>();
public static VideoManager instance = null;
public static VideoManager getInstance()
{
if (instance == null)
public VideoRecordData(VideoRecordData copyFrom)
{
instance = new VideoManager();
this.type = copyFrom.type;
this.videoUrl = copyFrom.videoUrl;
this.title = copyFrom.title;
this.description = copyFrom.description;
this.previewImageUrl = copyFrom.previewImageUrl;
this.previewImageHash = copyFrom.previewImageHash;
}
return instance;
}
public VideoManager()
{
initVideos();
}
public ArrayList<VideoRecord> getVideoList()
public static class VideoRecord extends VideoRecordData
{
ArrayList<VideoRecord> videos = new ArrayList<VideoRecord>();
synchronized(VideoArr)
public int id;
public VideoRecord(int id, VideoRecordData recordData)
{
videos.addAll(VideoArr);
super(recordData);
this.id = id;
}
return videos;
}
private void initVideos()
{
VideoArr.add(new VideoRecord("Rickroll", "http://www.youtube.com/watch?v=dQw4w9WgXcQ", "http://img.youtube.com/vi/dQw4w9WgXcQ/0.jpg", "youtube", "Rickroll"));
/*
BookArr.add(new BookRecord("Репка", "", "http://www.kitap-ufa.ru/upload/iblock/079/575.jpg", 40, "Русская народная сказка..."));
BookArr.add(new BookRecord("Уроки поэзии", "Рахматуллин С.И.", "http://www.kitap-ufa.ru/upload/iblock/6e7/012.jpg", 85, "В книгу вошли стихотворные произведения..."));
BookArr.add(new BookRecord("Новые валенки", "Калимуллина Г.К.", "http://www.kitap-ufa.ru/upload/iblock/edd/010.jpg", 75, "Детство - самая прекрасная, веселая и интересная пора в жизни человека..."));
BookArr.add(new BookRecord("В стране Урала", "Рафиков Б.З.", "", 90, "Эта книга настоящего мастера пера не оставит..."));
*/
}
@Override
public void InThreadAction(Bundle query, UpdateCallbackHolder callbackHolder)
{
ArrayList<String> imageToDownloadList = new ArrayList<String>();
for (int i = 0; i < VideoArr.size(); i++)
{
imageToDownloadList.add(VideoArr.get(i).imageUrl);
}
Bundle innerQuery = new Bundle();
innerQuery.putStringArray("imageUrlArr", imageToDownloadList.toArray(new String[imageToDownloadList.size()]));
ImageManager.getInstance().InThreadAction(innerQuery, callbackHolder);
callbackHolder.OnUpdated(null);
}
}

View File

@ -0,0 +1,331 @@
package fishrungames.bashgid.core.db;
import java.util.ArrayList;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class AlbumDataSource
{
private BashgidSqliteHelper dbHelper = null;
public AlbumDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void CreateNewAlbum(AlbumManager.AlbumRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
if (!innerIsAlbumAlreadyExist(recordData.name, database))
{
database.insert(BashgidSqliteHelper.TABLE_ALBUM, null, values);
} else
{
// Replace
}
} finally
{
dbHelper.close();
}
}
}
public boolean IsAlbumAlreadyExist(String name)
{
boolean result = false;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = innerIsAlbumAlreadyExist(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public void AddPhotoToAlbum(String imageUrl, String albumName)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, imageUrl);
values.put(BashgidSqliteHelper.COLUMN_NAME, albumName);
if (!innerPhotoAlbumRelationAlreadyExist(imageUrl, albumName, database))
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, null, values);
Log.e("a", "aaa2");
}
Log.e("a", "aaa3");
} finally
{
dbHelper.close();
}
}
}
public AlbumFullData GetAlbumFullData(String name)
{
AlbumFullData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = InnerGetAlbumFullData(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public AlbumFullData InnerGetAlbumFullData(String name, SQLiteDatabase database)
{
AlbumFullData result = null;
AlbumRecordData recordData = innerGetAlbumByName(name, database);
if (recordData != null)
{
result = new AlbumFullData(recordData);
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
for (String imageUrl : imageUrlArr)
{
result.photoRecordArr.add(MainActivity.getInstance().photoDataSource.InnerGetPhotoByImageUrl(imageUrl, database));
}
}
return result;
}
public AlbumShortData GetAlbumShortData(String name)
{
AlbumShortData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
AlbumRecordData recordData = innerGetAlbumByName(name, database);
if (recordData != null)
{
result = new AlbumShortData(recordData);
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
result.photoCount = imageUrlArr.size();
if (imageUrlArr.size() > 0)
{
result.firstImageUrl = imageUrlArr.get(0);
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
public ArrayList<AlbumShortData> GetAllAlbumShortData()
{
ArrayList<AlbumShortData> result = new ArrayList<AlbumShortData>();
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
/*
* Cursor cursor =
* database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[]
* { BashgidSqliteHelper.COLUMN_NAME,
* BashgidSqliteHelper.COLUMN_TITLE,
* BashgidSqliteHelper.COLUMN_DESCRIPTION },
* BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name
* }, null, null, null, null);
*/
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME,
BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION }, null, null, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
AlbumShortData shortData = new AlbumShortData(new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2)));
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(shortData.name, database);
shortData.photoCount = imageUrlArr.size();
if (imageUrlArr.size() > 0)
{
shortData.firstImageUrl = imageUrlArr.get(0);
}
result.add(shortData);
} while (cursor.moveToNext());
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
private boolean innerIsAlbumAlreadyExist(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_NAME
+ "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private boolean innerPhotoAlbumRelationAlreadyExist(String imageUrl, String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL,
BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?" + " AND " + BashgidSqliteHelper.COLUMN_NAME + "=?",
new String[] { imageUrl, name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private ArrayList<String> innerGetImageUrlArrInAlbum(String name, SQLiteDatabase database)
{
ArrayList<String> result = new ArrayList<String>();
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
result.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
return result;
}
public AlbumRecordData innerGetAlbumByName(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TITLE,
BashgidSqliteHelper.COLUMN_DESCRIPTION }, BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
return new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2));
}
}
return null;
}
}

View File

@ -0,0 +1,377 @@
package fishrungames.bashgid.core.db;
import java.util.ArrayList;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.ArticleManager;
import fishrungames.bashgid.core.ArticleManager.ArticleFullData;
import fishrungames.bashgid.core.ArticleManager.ArticleRecordData;
import fishrungames.bashgid.core.ArticleManager.ArticleShortData;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
import fishrungames.bashgid.core.ChannelManager.ChannelRecordData;
import fishrungames.bashgid.core.VideoManager.VideoRecordData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ArticleDataSource
{
// private SQLiteDatabase database = null;
private BashgidSqliteHelper dbHelper = null;
// private final Semaphore mutex = new Semaphore(1, true);
public ArticleDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void CreateNewArticle(ArticleManager.ArticleRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
values.put(BashgidSqliteHelper.COLUMN_TYPE, recordData.type);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_CONTENT, recordData.content);
values.put(BashgidSqliteHelper.COLUMN_GEOLAT, recordData.geoLat);
values.put(BashgidSqliteHelper.COLUMN_GEOLON, recordData.geoLon);
values.put(BashgidSqliteHelper.COLUMN_EXTERNAL_LINK, recordData.externalLink);
if (!innerIsArticleAlreadyExist(recordData.name, database))
{
database.insert(BashgidSqliteHelper.TABLE_ARTICLE, null, values);
} else
{
// Replace
}
} finally
{
dbHelper.close();
}
}
}
public boolean IsArticleAlreadyExist(String name)
{
boolean result = false;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = innerIsArticleAlreadyExist(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public void AddAlbumToArticle(String albumName, String articleName)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_ALBUM_NAME, albumName);
values.put(BashgidSqliteHelper.COLUMN_ARTICLE_NAME, articleName);
if (!innerAlbumArticleRelationAlreadyExist(albumName, articleName, database))
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_ALBUM_ARTICLE_RELATION, null, values);
Log.e("a", "aaa2");
}
Log.e("a", "aaa3");
} finally
{
dbHelper.close();
}
}
}
public void AddChannelToArticle(String channelName, String articleName)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_CHANNEL_NAME, channelName);
values.put(BashgidSqliteHelper.COLUMN_ARTICLE_NAME, articleName);
if (!innerChannelArticleRelationAlreadyExist(channelName, articleName, database))
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_CHANNEL_ARTICLE_RELATION, null, values);
Log.e("a", "aaa2");
}
Log.e("a", "aaa3");
} finally
{
dbHelper.close();
}
}
}
public ArticleFullData GetArticleFullData(String name)
{
ArticleFullData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ArticleRecordData recordData = innerGetArticleByName(name, database);
if (recordData != null)
{
result = new ArticleFullData(recordData);
ArrayList<String> albumNameArr = innerGetAlbumNameArrInArticle(name, database);
for (String albumName : albumNameArr)
{
result.albumArr.add(MainActivity.getInstance().albumDataSource.InnerGetAlbumFullData(albumName, database));
}
ArrayList<String> channelNameArr = innerGetChannelNameArrInArticle(name, database);
for (String channelName : channelNameArr)
{
result.channelArr.add(MainActivity.getInstance().channelDataSource.InnerGetChannelFullData(channelName, database));
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
public ArticleShortData GetArticleShortData(String name)
{
ArticleShortData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ArticleRecordData recordData = innerGetArticleByName(name, database);
if (recordData != null)
{
result = new ArticleShortData(recordData);
ArrayList<String> albumNameArr = innerGetAlbumNameArrInArticle(name, database);
if (albumNameArr.size() > 0)
{
result.previewImageUrl = albumNameArr.get(0);
}
else
{
ArrayList<String> channelNameArr = innerGetChannelNameArrInArticle(name, database);
if (channelNameArr.size() > 0)
{
ChannelFullData channelFullData = MainActivity.getInstance().channelDataSource.InnerGetChannelFullData(channelNameArr.get(0), database);
if (channelFullData.videoRecordArr.size() > 0)
{
result.previewImageUrl = channelFullData.videoRecordArr.get(0).previewImageUrl;
}
}
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
private boolean innerIsArticleAlreadyExist(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE, new String[] { BashgidSqliteHelper.COLUMN_NAME },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private boolean innerAlbumArticleRelationAlreadyExist(String albumName, String articleName, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM_ARTICLE_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ALBUM_NAME, BashgidSqliteHelper.COLUMN_ARTICLE_NAME },
BashgidSqliteHelper.COLUMN_ALBUM_NAME + "=?" + " AND " + BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?", new String[] { albumName, articleName }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private boolean innerChannelArticleRelationAlreadyExist(String channelName, String articleName, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL_ARTICLE_RELATION, new String[] { BashgidSqliteHelper.COLUMN_CHANNEL_NAME, BashgidSqliteHelper.COLUMN_ARTICLE_NAME },
BashgidSqliteHelper.COLUMN_CHANNEL_NAME + "=?" + " AND " + BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?", new String[] { channelName, articleName }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private ArticleRecordData innerGetArticleByName(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE, new String[] {
BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TYPE, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_CONTENT, BashgidSqliteHelper.COLUMN_GEOLAT, BashgidSqliteHelper.COLUMN_GEOLON, BashgidSqliteHelper.COLUMN_EXTERNAL_LINK },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
return new ArticleRecordData(
cursor.getString(0),
cursor.getInt(1),
cursor.getString(2),
cursor.getString(3),
cursor.getDouble(4),
cursor.getDouble(5),
cursor.getString(6)
);
}
}
return null;
}
private ArrayList<String> innerGetAlbumNameArrInArticle(String name, SQLiteDatabase database)
{
ArrayList<String> result = new ArrayList<String>();
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM_ARTICLE_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ALBUM_NAME },
BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
result.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
}
return result;
}
private ArrayList<String> innerGetChannelNameArrInArticle(String name, SQLiteDatabase database)
{
ArrayList<String> result = new ArrayList<String>();
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL_ARTICLE_RELATION, new String[] { BashgidSqliteHelper.COLUMN_CHANNEL_NAME },
BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
result.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
}
return result;
}
}

View File

@ -1,6 +1,9 @@
package fishrungames.bashgid.core.db;
import java.util.concurrent.Semaphore;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
@ -8,46 +11,185 @@ import android.util.Log;
public class BashgidSqliteHelper extends SQLiteOpenHelper
{
public static final String TABLE_NEWS = "table_news";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_IMAGE_ID = "imageId";
public static final String COLUMN_PUBDATE = "pubDate";
// Newstyle
public static final String TABLE_PHOTO = "table_photo";
public static final String TABLE_ALBUM = "table_album";
public static final String TABLE_PHOTO_ALBUM_RELATION = "table_photo_album_relation";
public static final String TABLE_ARTICLE = "table_article";
public static final String TABLE_ALBUM_ARTICLE_RELATION = "table_album_article_relation";
public static final String TABLE_CHANNEL_ARTICLE_RELATION = "table_channel_article_relation";
public static final String TABLE_VIDEO = "table_video";
public static final String TABLE_CHANNEL = "table_channel";
public static final String TABLE_VIDEO_CHANNEL_RELATION = "table_video_channel_relation";
public static final String TABLE_JOURNAL = "table_journal";
public static final String TABLE_ARTICLE_JOURNAL_RELATION = "table_article_journal_relation";
// Oldstyle
public static final String TABLE_NEWS = "table_news";
private static final String DATABASE_NAME = "bashgid.db";
private static final int DATABASE_VERSION = 1;
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_IMAGE_URL = "imageUrl";
public static final String COLUMN_IMAGE_HASH = "imageHash";
public static final String COLUMN_GEOLAT = "geoLat";
public static final String COLUMN_GEOLON = "geoLon";
public static final String COLUMN_PUBDATE = "pubDate";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_CONTENT = "content";
public static final String COLUMN_EXTERNAL_LINK = "externalLink";
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_NEWS +
"(" + COLUMN_ID + " integer primary key autoincrement" +
", " + COLUMN_TITLE + " text not null" +
", " + COLUMN_DESCRIPTION + " text not null" +
", " + COLUMN_IMAGE_ID + " text not null" +
", " + COLUMN_PUBDATE + " text not null" +
");";
public static final String COLUMN_ALBUM_NAME = "albumName";
public static final String COLUMN_CHANNEL_NAME = "channelName";
public static final String COLUMN_ARTICLE_NAME = "articleName";
public static final String COLUMN_JOURNAL_NAME = "journalName";
public static final String COLUMN_VIDEO_URL = "videoUrl";
public static final String COLUMN_PREVIEW_IMAGE_URL = "previewImageUrl";
public static final String COLUMN_PREVIEW_IMAGE_HASH = "previewImageHash";
private static final String DATABASE_NAME = "bashgid.db";
private static final int DATABASE_VERSION = 1;
public BashgidSqliteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Database creation sql statement
/*
private static final String DATABASE_CREATE = "create table " + TABLE_NEWS + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_TITLE
+ " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_IMAGE_URL + " text not null" + ", " + COLUMN_PUBDATE
+ " text not null" + ");";*/
private static final String TABLE_NEWS_CREATE = "create table " + TABLE_NEWS + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_TITLE
+ " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_IMAGE_URL + " text not null" + ", " + COLUMN_PUBDATE
+ " text not null" + ");";
private static final String TABLE_PHOTO_CREATE = "create table " + TABLE_PHOTO + "(" + COLUMN_ID + " integer primary key autoincrement"
+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_IMAGE_URL + " text not null" + ", " + COLUMN_IMAGE_HASH + " text not null" + ", " + COLUMN_GEOLAT
+ " real not null" + ", " + COLUMN_GEOLON + " real not null" + ");";
private static final String TABLE_ALBUM_CREATE = "create table " + TABLE_ALBUM + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ");";
private static final String TABLE_PHOTO_ALBUM_RELATION_CREATE = "create table " + TABLE_PHOTO_ALBUM_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_IMAGE_URL + " text not null"
+ ", " + COLUMN_NAME + " text not null" + ");";
private static final String TABLE_ARTICLE_CREATE = "create table " + TABLE_ARTICLE + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
+ ", " + COLUMN_TYPE + " integer not null" + ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_CONTENT + " text not null" + ", " + COLUMN_GEOLAT
+ " real not null" + ", " + COLUMN_GEOLON + " real not null" + ", " + COLUMN_EXTERNAL_LINK + " text not null" +");";
private static final String TABLE_ALBUM_ARTICLE_RELATION_CREATE = "create table " + TABLE_ALBUM_ARTICLE_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_ALBUM_NAME + " text not null"
+ ", " + COLUMN_ARTICLE_NAME + " text not null" + ");";
private static final String TABLE_CHANNEL_ARTICLE_RELATION_CREATE = "create table " + TABLE_CHANNEL_ARTICLE_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_CHANNEL_NAME + " text not null"
+ ", " + COLUMN_ARTICLE_NAME + " text not null" + ");";
private static final String TABLE_VIDEO_CREATE = "create table " + TABLE_VIDEO + "(" + COLUMN_ID + " integer primary key autoincrement"+ ", " + COLUMN_TYPE + " integer not null"
+ ", " + COLUMN_VIDEO_URL + " text not null" + ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ", " + COLUMN_PREVIEW_IMAGE_URL + " text not null"
+ ", " + COLUMN_PREVIEW_IMAGE_HASH + " text not null"+ ");";
private static final String TABLE_CHANNEL_CREATE = "create table " + TABLE_CHANNEL + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ");";
private static final String TABLE_VIDEO_CHANNEL_RELATION_CREATE = "create table " + TABLE_VIDEO_CHANNEL_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_VIDEO_URL + " text not null"
+ ", " + COLUMN_NAME + " text not null" + ");";
private static final String TABLE_JOURNAL_CREATE = "create table " + TABLE_JOURNAL + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_NAME + " text not null"
+ ", " + COLUMN_TITLE + " text not null" + ", " + COLUMN_DESCRIPTION + " text not null" + ");";
private static final String TABLE_ARTICLE_JOURNAL_RELATION_CREATE = "create table " + TABLE_ARTICLE_JOURNAL_RELATION + "(" + COLUMN_ID + " integer primary key autoincrement" + ", " + COLUMN_ARTICLE_NAME + " text not null"
+ ", " + COLUMN_JOURNAL_NAME + " text not null" + ");";
public final Semaphore mutex = new Semaphore(1, true);
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(BashgidSqliteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWS);
onCreate(db);
}
public BashgidSqliteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(TABLE_NEWS_CREATE);
database.execSQL(TABLE_PHOTO_CREATE);
database.execSQL(TABLE_ALBUM_CREATE);
database.execSQL(TABLE_PHOTO_ALBUM_RELATION_CREATE);
database.execSQL(TABLE_ARTICLE_CREATE);
database.execSQL(TABLE_ALBUM_ARTICLE_RELATION_CREATE);
database.execSQL(TABLE_CHANNEL_ARTICLE_RELATION_CREATE);
database.execSQL(TABLE_VIDEO_CREATE);
database.execSQL(TABLE_CHANNEL_CREATE);
database.execSQL(TABLE_VIDEO_CHANNEL_RELATION_CREATE);
database.execSQL(TABLE_JOURNAL_CREATE);
database.execSQL(TABLE_ARTICLE_JOURNAL_RELATION_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
//Log.w(BashgidSqliteHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALBUM);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTO_ALBUM_RELATION);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALBUM_ARTICLE_RELATION);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHANNEL_ARTICLE_RELATION);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHANNEL);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO_CHANNEL_RELATION);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_JOURNAL);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLE_JOURNAL_RELATION);
onCreate(db);
}
@Override
public SQLiteDatabase getWritableDatabase()
{
SQLiteDatabase result = null;
try
{
mutex.acquire();
try
{
result = super.getWritableDatabase();
} catch (Exception e)
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error in getWritableDatabase()", "Error in getWritableDatabase()");
}
return result;
}
@Override
public void close()
{
mutex.release();
super.close();
}
}

View File

@ -0,0 +1,288 @@
package fishrungames.bashgid.core.db;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import fishrungames.bashgid.core.ChannelManager;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;
import fishrungames.bashgid.core.ChannelManager.ChannelRecordData;
import fishrungames.bashgid.core.PhotoManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ChannelDataSource
{
private BashgidSqliteHelper dbHelper = null;
public ChannelDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void CreateNewChannel(ChannelManager.ChannelRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
if (!innerIsChannelAlreadyExist(recordData.name, database))
{
database.insert(BashgidSqliteHelper.TABLE_CHANNEL, null, values);
} else
{
// Replace
}
} finally
{
dbHelper.close();
}
}
}
public boolean IsChannelAlreadyExist(String name)
{
boolean result = false;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = innerIsChannelAlreadyExist(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public void AddVideoToChannel(String videoUrl, String channelName)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_VIDEO_URL, videoUrl);
values.put(BashgidSqliteHelper.COLUMN_NAME, channelName);
if (!innerVideoChannelRelationAlreadyExist(videoUrl, channelName, database))
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_VIDEO_CHANNEL_RELATION, null, values);
Log.e("a", "aaa2");
}
Log.e("a", "aaa3");
} finally
{
dbHelper.close();
}
}
}
public ChannelFullData GetChannelFullData(String name)
{
ChannelFullData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = InnerGetChannelFullData(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public ChannelFullData InnerGetChannelFullData(String name, SQLiteDatabase database)
{
ChannelFullData result = null;
ChannelRecordData recordData = InnerGetChannelByName(name, database);
if (recordData != null)
{
result = new ChannelFullData(recordData);
ArrayList<String> videoUrlArr = innerGetVideoUrlArrInAlbum(name, database);
for (String videoUrl : videoUrlArr)
{
result.videoRecordArr.add(MainActivity.getInstance().videoDataSource.InnerGetVideoByVideoUrl(videoUrl, database));
}
}
return result;
}
/*
public AlbumShortData GetAlbumShortData(String name)
{
AlbumShortData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
AlbumRecordData recordData = innerGetAlbumByName(name, database);
if (recordData != null)
{
result = new AlbumShortData(recordData);
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
result.photoCount = imageUrlArr.size();
if (imageUrlArr.size() > 0)
{
result.firstImageUrl = imageUrlArr.get(0);
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
*/
private boolean innerIsChannelAlreadyExist(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL, new String[] { BashgidSqliteHelper.COLUMN_NAME },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private boolean innerVideoChannelRelationAlreadyExist(String videoUrl, String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO_CHANNEL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_VIDEO_URL, BashgidSqliteHelper.COLUMN_NAME },
BashgidSqliteHelper.COLUMN_VIDEO_URL + "=?" + " AND " + BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { videoUrl, name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private ArrayList<String> innerGetVideoUrlArrInAlbum(String name, SQLiteDatabase database)
{
ArrayList<String> result = new ArrayList<String>();
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO_CHANNEL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_VIDEO_URL },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
result.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
}
return result;
}
public ChannelRecordData InnerGetChannelByName(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_CHANNEL,
new String[] { BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
return new ChannelRecordData(
cursor.getString(0),
cursor.getString(1),
cursor.getString(2)
);
}
}
return null;
}
}

View File

@ -0,0 +1,327 @@
package fishrungames.bashgid.core.db;
import java.util.ArrayList;
import fishrungames.bashgid.MainActivity;
import fishrungames.bashgid.core.AlbumManager;
import fishrungames.bashgid.core.AlbumManager.AlbumFullData;
import fishrungames.bashgid.core.AlbumManager.AlbumRecordData;
import fishrungames.bashgid.core.AlbumManager.AlbumShortData;
import fishrungames.bashgid.core.JournalManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class JournalDataSource
{
private BashgidSqliteHelper dbHelper = null;
public JournalDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void CreateNewJournal(JournalManager.JournalRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_NAME, recordData.name);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
if (!innerIsJournalAlreadyExist(recordData.name, database))
{
database.insert(BashgidSqliteHelper.TABLE_JOURNAL, null, values);
} else
{
// Replace
}
} finally
{
dbHelper.close();
}
}
}
public boolean IsJournalAlreadyExist(String name)
{
boolean result = false;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = innerIsJournalAlreadyExist(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public void AddArticleToJournal(String articleName, String journalName)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_ARTICLE_NAME, articleName);
values.put(BashgidSqliteHelper.COLUMN_JOURNAL_NAME, journalName);
if (!innerArticleJournalRelationAlreadyExist(articleName, journalName, database))
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, null, values);
Log.e("a", "aaa2");
}
Log.e("a", "aaa3");
} finally
{
dbHelper.close();
}
}
}
/*
public AlbumFullData GetAlbumFullData(String name)
{
AlbumFullData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = InnerGetAlbumFullData(name, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public AlbumFullData InnerGetAlbumFullData(String name, SQLiteDatabase database)
{
AlbumFullData result = null;
AlbumRecordData recordData = innerGetAlbumByName(name, database);
if (recordData != null)
{
result = new AlbumFullData(recordData);
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
for (String imageUrl : imageUrlArr)
{
result.photoRecordArr.add(MainActivity.getInstance().photoDataSource.InnerGetPhotoByImageUrl(imageUrl, database));
}
}
return result;
}
public AlbumShortData GetAlbumShortData(String name)
{
AlbumShortData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
AlbumRecordData recordData = innerGetAlbumByName(name, database);
if (recordData != null)
{
result = new AlbumShortData(recordData);
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(name, database);
result.photoCount = imageUrlArr.size();
if (imageUrlArr.size() > 0)
{
result.firstImageUrl = imageUrlArr.get(0);
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
public ArrayList<AlbumShortData> GetAllAlbumShortData()
{
ArrayList<AlbumShortData> result = new ArrayList<AlbumShortData>();
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME,
BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION }, null, null, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
AlbumShortData shortData = new AlbumShortData(new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2)));
ArrayList<String> imageUrlArr = innerGetImageUrlArrInAlbum(shortData.name, database);
shortData.photoCount = imageUrlArr.size();
if (imageUrlArr.size() > 0)
{
shortData.firstImageUrl = imageUrlArr.get(0);
}
result.add(shortData);
} while (cursor.moveToNext());
}
}
} finally
{
dbHelper.close();
}
}
return result;
}
*/
private boolean innerIsJournalAlreadyExist(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_JOURNAL, new String[] { BashgidSqliteHelper.COLUMN_NAME }, BashgidSqliteHelper.COLUMN_NAME
+ "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
private boolean innerArticleJournalRelationAlreadyExist(String articleName, String journalName, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ARTICLE_JOURNAL_RELATION, new String[] { BashgidSqliteHelper.COLUMN_ARTICLE_NAME,
BashgidSqliteHelper.COLUMN_JOURNAL_NAME }, BashgidSqliteHelper.COLUMN_ARTICLE_NAME + "=?" + " AND " + BashgidSqliteHelper.COLUMN_JOURNAL_NAME + "=?",
new String[] { articleName, journalName }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
/*
private ArrayList<String> innerGetImageUrlArrInAlbum(String name, SQLiteDatabase database)
{
ArrayList<String> result = new ArrayList<String>();
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO_ALBUM_RELATION, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
result.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
return result;
}
public AlbumRecordData innerGetAlbumByName(String name, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_ALBUM, new String[] { BashgidSqliteHelper.COLUMN_NAME, BashgidSqliteHelper.COLUMN_TITLE,
BashgidSqliteHelper.COLUMN_DESCRIPTION }, BashgidSqliteHelper.COLUMN_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
return new AlbumRecordData(cursor.getString(0), cursor.getString(1), cursor.getString(2));
}
}
return null;
}
*/
}

View File

@ -17,148 +17,60 @@ import android.util.Log;
public class NewsDataSource
{
//Xperimental -- move somewhere
// Xperimental -- move somewhere
public static final SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
private SQLiteDatabase database = null;
// private SQLiteDatabase database = null;
private BashgidSqliteHelper dbHelper = null;
private String[] allColumns = { BashgidSqliteHelper.COLUMN_ID, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION,
BashgidSqliteHelper.COLUMN_IMAGE_ID, BashgidSqliteHelper.COLUMN_PUBDATE };
private final Semaphore mutex = new Semaphore(1, true);
BashgidSqliteHelper.COLUMN_IMAGE_URL, BashgidSqliteHelper.COLUMN_PUBDATE };
public NewsDataSource(Context context)
public NewsDataSource(Context context, BashgidSqliteHelper dbHelper)
{
try
{
mutex.acquire();
try
{
dbHelper = new BashgidSqliteHelper(context);
} finally
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error when creating BashgidSqliteHelper", "Error when creating BashgidSqliteHelper");
}
}
public void open() throws SQLException
{
try
{
mutex.acquire();
try
{
database = dbHelper.getWritableDatabase();
} finally
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error when opening NewsDataSource", "Error when opening NewsDataSource");
database = null;
}
this.dbHelper = dbHelper;
}
public void close()
{
try
{
mutex.acquire();
try
{
if (dbHelper != null)
{
dbHelper.close();
database = null;
}
} finally
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error when closing NewsDataSource", "Error when closing NewsDataSource");
}
}
public void createNewsRecord(NewsManager.NewsRecord newsRecord)
private void createNewsRecord(SQLiteDatabase database, NewsManager.NewsRecord newsRecord)
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_TITLE, newsRecord.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, newsRecord.description);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_ID, newsRecord.imageId);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, newsRecord.imageId);
values.put(BashgidSqliteHelper.COLUMN_PUBDATE, iso8601Format.format(newsRecord.pubDate));
try
{
mutex.acquire();
try
{
if (database != null)
{
database.insert(BashgidSqliteHelper.TABLE_NEWS, null, values);
}
} finally
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error when calling createNewsRecord", "Error when calling createNewsRecord");
}
database.insert(BashgidSqliteHelper.TABLE_NEWS, null, values);
}
public void replaceNews(ArrayList<NewsManager.NewsRecord> newsRecordArr)
{
try
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
mutex.acquire();
try
{
if (database != null)
{
database.delete(BashgidSqliteHelper.TABLE_NEWS, null, null);
}
} finally
{
mutex.release();
}
} catch (InterruptedException ie)
{
Log.e("Error when calling replaceNews", "Error when calling replaceNews");
}
database.delete(BashgidSqliteHelper.TABLE_NEWS, null, null);
for (NewsManager.NewsRecord newsRecord : newsRecordArr)
{
createNewsRecord(newsRecord);
for (NewsManager.NewsRecord newsRecord : newsRecordArr)
{
createNewsRecord(database, newsRecord);
}
dbHelper.close();
}
finally
{
dbHelper.close();
}
}
}
@ -166,80 +78,70 @@ public class NewsDataSource
public ArrayList<NewsManager.NewsRecord> getNews()
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
ArrayList<NewsManager.NewsRecord> newsRecordArr = new ArrayList<NewsManager.NewsRecord>();
try
if (database != null)
{
mutex.acquire();
try
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_NEWS, allColumns, null, null, null, null, null);
if (database != null)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_NEWS, allColumns, null, null, null, null, null);
cursor.moveToFirst();
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
NewsManager.NewsRecord newsRecord = cursorToNewsRecord(cursor);
newsRecordArr.add(newsRecord);
cursor.moveToNext();
}
cursor.close();
}
} finally
while (!cursor.isAfterLast())
{
mutex.release();
NewsManager.NewsRecord newsRecord = cursorToNewsRecord(cursor);
newsRecordArr.add(newsRecord);
cursor.moveToNext();
}
cursor.close();
}
finally
{
dbHelper.close();
}
} catch (InterruptedException ie)
{
Log.e("Error when calling getNews", "Error when calling getNews");
}
return newsRecordArr;
}
public ArrayList<NewsManager.NewsRecord> searchNewsByString(String searchText)
{
ArrayList<NewsManager.NewsRecord> newsRecordArr = new ArrayList<NewsManager.NewsRecord>();
try
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
mutex.acquire();
try
{
Cursor cursor = database.query(true, BashgidSqliteHelper.TABLE_NEWS, allColumns, BashgidSqliteHelper.COLUMN_TITLE + " LIKE '%" + searchText + "%'",
null, null, null, null, null);
if (database != null)
{
Cursor cursor = database.query(true, BashgidSqliteHelper.TABLE_NEWS, allColumns, BashgidSqliteHelper.COLUMN_TITLE + " LIKE '%"+searchText+"%'", null, null, null, null, null);
cursor.moveToFirst();
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
NewsManager.NewsRecord newsRecord = cursorToNewsRecord(cursor);
newsRecordArr.add(newsRecord);
cursor.moveToNext();
}
cursor.close();
}
} finally
while (!cursor.isAfterLast())
{
mutex.release();
NewsManager.NewsRecord newsRecord = cursorToNewsRecord(cursor);
newsRecordArr.add(newsRecord);
cursor.moveToNext();
}
} catch (InterruptedException ie)
{
Log.e("Error when calling searchNewsByString", "Error when calling searchNewsByString");
cursor.close();
}
finally
{
dbHelper.close();
}
}
return newsRecordArr;
@ -252,8 +154,7 @@ public class NewsDataSource
try
{
newsRecord = new NewsManager.NewsRecord(cursor.getString(1), cursor.getString(2), cursor.getString(3), iso8601Format.parse(cursor.getString(4)));
}
catch (ParseException e)
} catch (ParseException e)
{
e.printStackTrace();
}

View File

@ -0,0 +1,137 @@
package fishrungames.bashgid.core.db;
import fishrungames.bashgid.core.PhotoManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class PhotoDataSource
{
// private SQLiteDatabase database = null;
private BashgidSqliteHelper dbHelper = null;
// private String[] allColumns = { BashgidSqliteHelper.COLUMN_ID,
// BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION,
// BashgidSqliteHelper.COLUMN_IMAGE_URL, BashgidSqliteHelper.COLUMN_GEOLAT,
// BashgidSqliteHelper.COLUMN_GEOLON };
public PhotoDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void AddOrReplacePhoto(PhotoManager.PhotoRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
//values.put(BashgidSqliteHelper.COLUMN_ID, 1);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_URL, recordData.imageUrl);
values.put(BashgidSqliteHelper.COLUMN_IMAGE_HASH, recordData.imageHash);
values.put(BashgidSqliteHelper.COLUMN_GEOLAT, recordData.geoLat);
values.put(BashgidSqliteHelper.COLUMN_GEOLON, recordData.geoLon);
boolean r = isPhotoAlreadyExist(recordData.imageUrl, database);
if (!r)
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_PHOTO, null, values);
Log.e("a", "aaa2");
} else
{
// Replace
Log.e("a", "aaa3");
}
Log.e("a", "aaa4");
} finally
{
dbHelper.close();
}
}
}
public PhotoManager.PhotoRecordData GetPhotoByImageUrl(String imageUrl)
{
PhotoManager.PhotoRecordData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = InnerGetPhotoByImageUrl(imageUrl, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public PhotoManager.PhotoRecordData InnerGetPhotoByImageUrl(String imageUrl, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO,
new String[] { BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_IMAGE_URL, BashgidSqliteHelper.COLUMN_IMAGE_HASH, BashgidSqliteHelper.COLUMN_GEOLAT, BashgidSqliteHelper.COLUMN_GEOLON },
BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
return new PhotoManager.PhotoRecordData(
cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getDouble(4),
cursor.getDouble(5)
);
}
}
return null;
}
private boolean isPhotoAlreadyExist(String imageUrl, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_PHOTO, new String[] { BashgidSqliteHelper.COLUMN_IMAGE_URL },
BashgidSqliteHelper.COLUMN_IMAGE_URL + "=?", new String[] { imageUrl }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,131 @@
package fishrungames.bashgid.core.db;
import fishrungames.bashgid.core.VideoManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class VideoDataSource
{
private BashgidSqliteHelper dbHelper = null;
public VideoDataSource(Context context, BashgidSqliteHelper dbHelper)
{
this.dbHelper = dbHelper;
}
public void AddOrReplaceVideo(VideoManager.VideoRecordData recordData)
{
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
ContentValues values = new ContentValues();
values.put(BashgidSqliteHelper.COLUMN_TYPE, recordData.type);
values.put(BashgidSqliteHelper.COLUMN_TITLE, recordData.title);
values.put(BashgidSqliteHelper.COLUMN_DESCRIPTION, recordData.description);
values.put(BashgidSqliteHelper.COLUMN_VIDEO_URL, recordData.videoUrl);
values.put(BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_URL, recordData.previewImageUrl);
values.put(BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_HASH, recordData.previewImageHash);
boolean r = isVideoAlreadyExist(recordData.videoUrl, database);
if (!r)
{
Log.e("a", "aaa1");
database.insert(BashgidSqliteHelper.TABLE_VIDEO, null, values);
Log.e("a", "aaa2");
} else
{
// Replace
Log.e("a", "aaa3");
}
Log.e("a", "aaa4");
} finally
{
dbHelper.close();
}
}
}
public VideoManager.VideoRecordData GetVideoByVideoUrl(String videoUrl)
{
VideoManager.VideoRecordData result = null;
SQLiteDatabase database = dbHelper.getWritableDatabase();
if (database != null)
{
try
{
result = InnerGetVideoByVideoUrl(videoUrl, database);
} finally
{
dbHelper.close();
}
}
return result;
}
public VideoManager.VideoRecordData InnerGetVideoByVideoUrl(String videoUrl, SQLiteDatabase database)
{
VideoManager.VideoRecordData result = null;
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO,
new String[] { BashgidSqliteHelper.COLUMN_TYPE, BashgidSqliteHelper.COLUMN_VIDEO_URL, BashgidSqliteHelper.COLUMN_TITLE, BashgidSqliteHelper.COLUMN_DESCRIPTION, BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_URL, BashgidSqliteHelper.COLUMN_PREVIEW_IMAGE_HASH },
BashgidSqliteHelper.COLUMN_VIDEO_URL + "=?", new String[] { videoUrl }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
result = new VideoManager.VideoRecordData(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5)
);
}
}
return result;
}
private boolean isVideoAlreadyExist(String videoUrl, SQLiteDatabase database)
{
Cursor cursor = database.query(BashgidSqliteHelper.TABLE_VIDEO, new String[] { BashgidSqliteHelper.COLUMN_VIDEO_URL },
BashgidSqliteHelper.COLUMN_VIDEO_URL + "=?", new String[] { videoUrl }, null, null, null, null);
if (cursor != null)
{
if (cursor.getCount() > 0)
{
return true;
}
}
return false;
}
}