Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. public class MosaicMenuActivity extends Activity {
  2. public final String TAG = "NoniusDroidBaseApp";
  3.  
  4. // The title image (on top of the window)
  5. private String TITLE_IMAGE = "title/title_MAIN";
  6. // The background (normally you don't want to change it)
  7. private String BACKAGROUND_IMAGE = "background";
  8.  
  9. // The content loader is used to load the application on another thread
  10. // It makes sure that you can't interact with the application until the load
  11. // is completed,
  12. // but it will also blocks allows you to exit the application while it it
  13. // loading.
  14. // The interface will also never block
  15. private PageContentLoader content_loader;
  16.  
  17. // The content adapter holds one application content array.
  18. private PagesContentAdapter content_adapter;
  19.  
  20. // Actual system language
  21. private String language;
  22.  
  23. /*
  24. * (non-Javadoc)
  25. *
  26. * @see android.app.Activity#onCreate(android.os.Bundle)
  27. */
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. // Apply the main.xml as the content view
  32. setContentView(R.layout.main);
  33. // Apply the Nonius default theme
  34. getApplicationContext().setTheme(R.style.Theme);
  35. }
  36.  
  37. @Override
  38. public void onResume() {
  39. super.onResume();
  40.  
  41. // Whenever the application resumes, the loader check if the Theme
  42. // changed (or the language was modified)
  43. // If it was modified, it loads the theme again
  44. if (ThemeManager.themeModified(language)) {
  45. loadTheme();
  46. }
  47.  
  48. // Get the actual system language
  49. language = SetTopBoxDetails.getLanguage();
  50.  
  51. // Build the content adapter
  52. this.content_adapter = new PagesContentAdapter(
  53. this.getApplicationContext(), language);
  54.  
  55. // Applies the content adapter to the content ViewPager
  56. ViewPager myPager = (ViewPager) findViewById(R.id.scrollview_pages);
  57. myPager.setAdapter(content_adapter);
  58. myPager.setCurrentItem(0);
  59.  
  60. // Builds the module that will load the content from the xml
  61. this.content_loader = new PageContentLoader(this, content_adapter, language);
  62. // The content is read whenever the application resumes
  63. loadDynamicContent();
  64. }
  65.  
  66.  
  67. public boolean onKeyDown(int keyCode, KeyEvent event){
  68. Log.d(TAG, "onKeyDown");
  69. return super.onKeyDown(keyCode, event);
  70. }
  71.  
  72. /*
  73. * This function is used to load content that comes from outside of the
  74. * application.
  75. */
  76. private void loadDynamicContent() {
  77. // The content loader makes sure that the user can't interact with the
  78. // application until the load is done.
  79. this.content_loader.execute();
  80. }
  81.  
  82. /*
  83. * Loads the application theme.
  84. */
  85. private void loadTheme() {
  86. // Get the context
  87. Context context = this.getApplicationContext();
  88.  
  89. // Load the background
  90. RelativeLayout relativelayout_base = (RelativeLayout) findViewById(R.id.relativelayout_base);
  91. relativelayout_base.setBackgroundDrawable(ThemeManager.getImage(
  92. BACKAGROUND_IMAGE, context));
  93. }
  94.  
  95. public class PageContentLoader extends AsyncTask<Void, Integer, Boolean> {
  96. public static final String TAG = "PageContentLoader";
  97. private static int SLEEPING_TIME = 2000;
  98.  
  99. // The location of the XML file
  100. private static final String XML_PATH = "/data/nonius/menu/mosaic.xml";
  101.  
  102. // XML node keys used to parse xml
  103. private static final String KEY_PAGE = "page";
  104. private static final String KEY_PAGE_TEMPLATE = "template";
  105. private static final String KEY_TILE = "tile";
  106.  
  107. // The parent activity. Used to lauch UI items
  108. private MosaicMenuActivity activity;
  109.  
  110. // The content storage
  111. private PagesContentAdapter content_adapter;
  112.  
  113. // The dialog that locks the user interaction
  114. private LoaderDialog dialog;
  115.  
  116. // The array of content (in this demo, Pages!)
  117. public ArrayList<Page> pages;
  118.  
  119. private String language;
  120.  
  121. public PageContentLoader(MosaicMenuActivity activity,
  122. PagesContentAdapter content_adapter, String language) {
  123. this.activity = activity;
  124. this.pages = new ArrayList<Page>();
  125. this.dialog = new LoaderDialog(this.activity);
  126. this.content_adapter = content_adapter;
  127. this.language = language;
  128. }
  129.  
  130. public boolean loadXml() {
  131. Log.d(TAG, "loadXml");
  132. // Clean the pages array
  133. this.pages = new ArrayList<Page>();
  134.  
  135. // Get the DOM element
  136. Document doc = XmlHelper.getDomElement(XML_PATH);
  137. // If the document is null
  138. if (doc == null) {
  139. // Return fail
  140. return false;
  141. }
  142.  
  143. // Get all the nodes with tag KEY_PAGE
  144. NodeList page_nodelist = doc.getElementsByTagName(KEY_PAGE);
  145. Log.d(TAG, "getElementsByTagName");
  146.  
  147. // looping through all item nodes <item>
  148. for (int page_i = 0; page_i < page_nodelist.getLength(); page_i++) {
  149. Page page = new Page();
  150.  
  151. Log.d(TAG, "Page " + page_i);
  152. // Get the element of the node list
  153. Element page_element = (Element) page_nodelist.item(page_i);
  154.  
  155. String page_template = page_element.getAttribute(KEY_PAGE_TEMPLATE);
  156. page.setTemplate(page_template);
  157.  
  158. NodeList tile_nodelist = page_element
  159. .getElementsByTagName(KEY_TILE);
  160.  
  161. for (int tile_i = 0; tile_i < tile_nodelist.getLength(); tile_i++){
  162. // Create an empty tile
  163. Tile tile = new Tile();
  164.  
  165. Element tile_element = (Element) tile_nodelist.item(tile_i);
  166. tile.importXmlElement(tile_element);
  167.  
  168. page.addTile(tile);
  169.  
  170. Log.d(TAG, tile.toString());
  171. }
  172.  
  173. this.pages.add(page);
  174. }
  175.  
  176. // Everything went ok
  177. return true;
  178. }
  179.  
  180. protected void onPreExecute() {
  181. this.dialog.setLoadMode(activity.getString(R.string.loading_message));
  182. this.dialog.show();
  183. }
  184.  
  185. protected Boolean doInBackground(Void... params) {
  186. // Parse the xml file
  187. return this.loadXml();
  188. }
  189.  
  190. protected void onPostExecute(Boolean success) {
  191. // If the loader succeeded
  192. if (success) {
  193. // Update the adapter
  194. content_adapter.updateContent(pages);
  195.  
  196. // If the dialog is visible, hide it
  197. if (dialog.isShowing()) {
  198. dialog.dismiss();
  199. }
  200. }
  201. // If the loader failed
  202. else {
  203. // Start the error dialog
  204. dialog.setErrorMode(activity
  205. .getString(R.string.loading_error_message));
  206. }
  207. }
  208. }
  209.  
  210. public class PagesContentAdapter extends PagerAdapter {
  211. public static final String TAG = "PagesContentAdapter";
  212. // The list context
  213. private Context context;
  214.  
  215. // The content array
  216. public ArrayList<Page> pages;
  217.  
  218. // The system language
  219. private String language;
  220.  
  221. /**
  222. * Class constructor
  223. *
  224. * @param context
  225. * The context used to build the ouput views
  226. */
  227. public PagesContentAdapter(Context context, String language) {
  228. this.context = context;
  229. // Create an empty pages array
  230. this.pages = new ArrayList<Page>();
  231. this.language = language;
  232. }
  233.  
  234. /**
  235. * Update the Adapter pages array and notify the parent
  236. * that the data was modified
  237. * @param pages
  238. */
  239. public void updateContent(ArrayList<Page> pages) {
  240. this.pages = pages;
  241. //this.notifyDataSetChanged();
  242. }
  243.  
  244. public int getCount() {
  245. return pages.size();
  246. }
  247.  
  248. public Object getItem(int position) {
  249. return pages.get(position);
  250. }
  251.  
  252. public long getItemId(int position) {
  253. return position;
  254. }
  255.  
  256. @Override
  257. public void destroyItem(View collection, int position, Object view) {
  258. ((ViewPager) collection).removeView((View) view);
  259.  
  260. }
  261.  
  262. @Override
  263. public void finishUpdate(View arg0) {
  264. // TODO Auto-generated method stub
  265.  
  266. }
  267.  
  268. @Override
  269. public Object instantiateItem(View page_view, int position) {
  270. Page page = new Page();
  271. page = pages.get(position);
  272.  
  273. LayoutInflater page_layout_inflater = (LayoutInflater) context
  274. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  275. LinearLayout linearlayout_page = (LinearLayout) page_layout_inflater.inflate(R.layout.page_template_5icons_v1, null);
  276.  
  277. for(int i=0; i<5; i++) {
  278. linearlayout_page.addView(layoutTile(linearlayout_page, page, i));
  279. }
  280.  
  281. ((ViewPager) page_view).addView(linearlayout_page, 0);
  282.  
  283. return linearlayout_page;
  284. }
  285.  
  286. public RelativeLayout layoutTile(LinearLayout linearlayout_page, Page page, int index) {
  287. RelativeLayout relativelayout_tile = null;
  288.  
  289. switch(index) {
  290. case 0: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_big_1);
  291. case 1: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_1);
  292. case 2: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_2);
  293. case 3: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_3);
  294. case 4: relativelayout_tile = (RelativeLayout) linearlayout_page.findViewById(R.id.item_small_4);
  295. }
  296.  
  297. // Inflate it in a layout xml
  298. LayoutInflater tile1_layout_inflater = (LayoutInflater) context
  299. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  300. tile1_layout_inflater.inflate(R.layout.tile, relativelayout_tile);
  301. // Get the textview from the inflated layout
  302. TextView textview_tail = (TextView) relativelayout_tile.findViewById(R.id.textview_tile);
  303. Tile tile = page.getTiles().get(index);
  304. // Set its text
  305. textview_tail.setText(tile.getDescription());
  306.  
  307. return relativelayout_tile;
  308. }
  309.  
  310. @Override
  311. public boolean isViewFromObject(View view, Object object) {
  312. return view == object;
  313. }
  314.  
  315. @Override
  316. public void restoreState(Parcelable arg0, ClassLoader arg1) {
  317. // TODO Auto-generated method stub
  318.  
  319. }
  320.  
  321. @Override
  322. public Parcelable saveState() {
  323. // TODO Auto-generated method stub
  324. return null;
  325. }
  326.  
  327. @Override
  328. public void startUpdate(View arg0) {
  329. // TODO Auto-generated method stub
  330.  
  331. }
  332.  
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement