Advertisement
minafaw3

leaynik Home

Oct 1st, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.68 KB | None | 0 0
  1. package com.imaadv.leaynik.Activities;
  2.  
  3. /*
  4. * Copyright (C) 2014 Chris Renke
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18.  
  19. import android.app.AlertDialog;
  20. import android.app.Dialog;
  21. import android.content.DialogInterface;
  22. import android.content.Intent;
  23. import android.content.res.Resources;
  24. import android.content.res.TypedArray;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.support.v4.app.ActivityCompat;
  28. import android.support.v4.app.Fragment;
  29. import android.support.v4.app.FragmentActivity;
  30. import android.support.v4.app.FragmentManager;
  31. import android.support.v4.widget.DrawerLayout;
  32. import android.view.Gravity;
  33. import android.view.View;
  34. import android.view.View.OnClickListener;
  35. import android.widget.AdapterView;
  36. import android.widget.AdapterView.OnItemClickListener;
  37. import android.widget.Button;
  38. import android.widget.ImageView;
  39. import android.widget.LinearLayout;
  40. import android.widget.ListView;
  41. import android.widget.TextView;
  42.  
  43. import com.imaadv.leaynik.R;
  44. import com.imaadv.leaynik.api.API;
  45. import com.imaadv.leaynik.defs.AppConstants;
  46. import com.imaadv.leaynik.defs.DrawerItem;
  47. import com.imaadv.leaynik.test.Test_Home;
  48. import com.imaadv.leaynik.ui.DrawerAdapter;
  49.  
  50. import java.util.ArrayList;
  51.  
  52.  
  53. public class Home extends FragmentActivity {
  54.  
  55. private final static int DAYS_UNTIL_PROMPT = 3;
  56. private final static int LAUNCHES_UNTIL_PROMPT = 7;
  57.  
  58. private DrawerArrowDrawable drawerArrowDrawable;
  59. private float offset;
  60. private boolean flipped;
  61. private ListView DrawerList;
  62. private DrawerLayout drawer;
  63. private TextView styleButton;
  64. private API api;
  65.  
  66. private String[] nav_items;
  67. private TypedArray nav_icons;
  68.  
  69. @Override
  70. protected void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. // requestWindowFeature(Window.FEATURE_NO_TITLE);
  73. // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  74. // WindowManager.LayoutParams.FLAG_FULLSCREEN);
  75. setContentView(R.layout.home_view);
  76. api = new API(this);
  77. if (!api.getSetting(AppConstants.ALERT).contentEquals(AppConstants.TRUE))
  78. openAlert();
  79.  
  80. DrawerList = (ListView) findViewById(R.id.DrawerList);
  81. ArrayList<DrawerItem> Items = new ArrayList<DrawerItem>(9);
  82.  
  83. Resources res= getResources();
  84.  
  85. nav_items = res.getStringArray(R.array.nav_items);
  86. nav_icons = res.obtainTypedArray(R.array.nav_icon);
  87.  
  88. for(int i=0 ; i<nav_items.length ; i++){
  89. Items.add(new DrawerItem(nav_items[i] , nav_icons.getResourceId(i,-1)));
  90.  
  91. }
  92. // Items.add(new DrawerItem(getResources().getString(R.string.tests),
  93. // R.drawable.test));
  94. // Items.add(new DrawerItem(getResources().getString(R.string.disease),
  95. // R.drawable.ill));
  96. // Items.add(new DrawerItem(getResources().getString(R.string.questions),
  97. // R.drawable.questions));
  98. // Items.add(new DrawerItem(getResources().getString(R.string.clincs),
  99. // R.drawable.location));
  100. // Items.add(new DrawerItem(getResources().getString(R.string.medtime),
  101. // R.drawable.alram));
  102. // Items.add(new DrawerItem(getResources().getString(R.string.articales),
  103. // R.drawable.articles));
  104. // Items.add(new DrawerItem(getResources().getString(R.string.profile),
  105. // R.drawable.profile));
  106. // Items.add(new DrawerItem(getResources().getString(R.string.compition),
  107. // R.drawable.quiz));
  108. //
  109. // Items.add(new DrawerItem(getResources().getString(R.string.logout),
  110. // R.drawable.logout));
  111. DrawerAdapter adapter = new DrawerAdapter(this, Items);
  112.  
  113. DrawerList.setAdapter(adapter);
  114.  
  115. DrawerList.setOnItemClickListener(new OnItemClickListener() {
  116.  
  117. @Override
  118. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  119. long arg3) {
  120.  
  121. SelectItem(arg2);
  122. }
  123. });
  124. drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  125. final ImageView imageView = (ImageView) findViewById(R.id.drawer_indicator);
  126. final Resources resources = getResources();
  127.  
  128. drawerArrowDrawable = new DrawerArrowDrawable(resources);
  129. drawerArrowDrawable.setStrokeColor(resources
  130. .getColor(R.color.light_gray));
  131. imageView.setImageDrawable(drawerArrowDrawable);
  132.  
  133. drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
  134. @Override
  135. public void onDrawerSlide(View drawerView, float slideOffset) {
  136. offset = slideOffset;
  137.  
  138. // Sometimes slideOffset ends up so close to but not quite 1 or
  139. // 0.
  140. if (slideOffset >= .995) {
  141. flipped = true;
  142. drawerArrowDrawable.setFlip(flipped);
  143. } else if (slideOffset <= .005) {
  144. flipped = false;
  145. drawerArrowDrawable.setFlip(flipped);
  146. }
  147.  
  148. drawerArrowDrawable.setParameter(offset);
  149. }
  150. });
  151.  
  152. openDrawer();
  153.  
  154. imageView.setOnClickListener(new View.OnClickListener() {
  155. @Override
  156. public void onClick(View v) {
  157. // if (drawer.isDrawerVisible(5)) {
  158. // drawer.closeDrawer(5);
  159. // } else {
  160. // drawer.openDrawer(5);
  161. // }
  162. openDrawer();
  163. }
  164. });
  165.  
  166. styleButton = (TextView) findViewById(R.id.indicator_style);
  167.  
  168. FragmentManager fragmentManager = getSupportFragmentManager();
  169.  
  170. int commit = fragmentManager
  171. .beginTransaction()
  172. .replace(R.id.view_content, Test_Home.newInstance(), "FRAGMENT")
  173. .addToBackStack(null).commit();
  174. DrawerList.setItemChecked(0, true);
  175.  
  176. // drawer.closeDrawer(5);
  177.  
  178. int numberofentrance = Integer.parseInt(api.getSetting("launch_count")) + 1;
  179. api.setSetting("launch_count", String.valueOf(numberofentrance));
  180. Long date_firstLaunch = Long.parseLong(api
  181. .getSetting("date_firstlaunch"));
  182. if (date_firstLaunch == 0) {
  183. date_firstLaunch = System.currentTimeMillis();
  184. api.setSetting("date_firstlaunch", String.valueOf(date_firstLaunch));
  185. }
  186.  
  187. if (numberofentrance >= LAUNCHES_UNTIL_PROMPT) {
  188. if (System.currentTimeMillis() >= date_firstLaunch
  189. + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
  190. showRateDialog();
  191. }
  192. }
  193.  
  194. }
  195.  
  196. public void showRateDialog() {
  197. final Dialog dialog = new Dialog(this);
  198. dialog.setTitle("Rate " + getResources().getString(R.string.app_name));
  199.  
  200. LinearLayout ll = new LinearLayout(this);
  201. ll.setOrientation(LinearLayout.VERTICAL);
  202.  
  203. TextView tv = new TextView(this);
  204. tv.setText("If you enjoy using "
  205. + getResources().getString(R.string.app_name)
  206. + ", please take a moment to rate it. Thanks for your support!");
  207. tv.setWidth(240);
  208. tv.setPadding(4, 0, 4, 10);
  209. ll.addView(tv);
  210.  
  211. Button b1 = new Button(this);
  212. b1.setText("Rate " + getResources().getString(R.string.app_name));
  213. b1.setOnClickListener(new OnClickListener() {
  214. public void onClick(View v) {
  215. startActivity(new Intent(Intent.ACTION_VIEW, Uri
  216. .parse("market://details?id=" + "com.imaadv.leaynik")));
  217. dialog.dismiss();
  218. }
  219. });
  220. ll.addView(b1);
  221.  
  222. Button b2 = new Button(this);
  223. b2.setText("Remind me later");
  224. b2.setOnClickListener(new OnClickListener() {
  225. public void onClick(View v) {
  226. dialog.dismiss();
  227. }
  228. });
  229. ll.addView(b2);
  230.  
  231. Button b3 = new Button(this);
  232.  
  233. b3.setText("No, thanks");
  234. b3.setOnClickListener(new OnClickListener() {
  235. public void onClick(View v) {
  236. api.setSetting(AppConstants.DONTSHOWAGAIN, AppConstants.TRUE);
  237.  
  238. dialog.dismiss();
  239. }
  240. });
  241. ll.addView(b3);
  242.  
  243. dialog.setContentView(ll);
  244. dialog.show();
  245. }
  246.  
  247. private void openDrawer() {
  248. if (drawer.isDrawerVisible(Gravity.RIGHT)) {
  249. drawer.closeDrawer(Gravity.RIGHT);
  250. } else {
  251. drawer.openDrawer(Gravity.RIGHT);
  252. }
  253. }
  254.  
  255. @Override
  256. public void onBackPressed() {
  257. FragmentManager manager = getSupportFragmentManager();
  258. int count = manager.getBackStackEntryCount();
  259. Test_Home fragement = (Test_Home) getSupportFragmentManager()
  260. .findFragmentByTag("FRAGMENT");
  261.  
  262. if (count == 0) {
  263. // if (fragement != null)
  264. if (fragement.isVisible()) {
  265. // super.onBackPressed();
  266. Home.this.finish();
  267. // System.exit(0);
  268. } else {
  269. // super.onBackPressed();
  270. }
  271. } else {
  272. if (api.getSetting("zft").contentEquals("zft")) {
  273. api.setSetting("zft", "");
  274. // Home.this.finish();
  275. ActivityCompat.finishAffinity(this);
  276. } else
  277. manager.popBackStack();
  278. //
  279. }
  280.  
  281. styleButton.setText(getResources().getString(R.string.app_name));
  282. }
  283.  
  284. private void openAlert() {
  285. api.setSetting(AppConstants.ALERT, AppConstants.TRUE);
  286. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  287. Home.this);
  288.  
  289. alertDialogBuilder
  290. .setTitle(getResources().getString(R.string.app_name));
  291. alertDialogBuilder.setMessage(getResources().getString(R.string.sos));
  292. // set positive button: Yes message
  293. alertDialogBuilder.setPositiveButton(
  294. getResources().getString(R.string.ok),
  295. new DialogInterface.OnClickListener() {
  296. public void onClick(DialogInterface dialog, int id) {
  297.  
  298. }
  299. });
  300.  
  301. AlertDialog alertDialog = alertDialogBuilder.create();
  302. // show alert
  303. alertDialog.show();
  304. }
  305.  
  306.  
  307. private void Logout() {
  308.  
  309. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  310. Home.this);
  311.  
  312. alertDialogBuilder
  313. .setTitle(getResources().getString(R.string.app_name));
  314. alertDialogBuilder.setMessage(getResources().getString(
  315. R.string.logout_msg));
  316. // set positive button: Yes message
  317. alertDialogBuilder.setPositiveButton(
  318. getResources().getString(R.string.ok),
  319. new DialogInterface.OnClickListener() {
  320. public void onClick(DialogInterface dialog, int id) {
  321. if (api == null) {
  322. api = new API(Home.this);
  323. }
  324. api.clearSettings();
  325. finish();
  326. }
  327. });
  328. alertDialogBuilder.setNegativeButton(
  329. getResources().getString(R.string.cancel),
  330. new DialogInterface.OnClickListener() {
  331.  
  332. @Override
  333. public void onClick(DialogInterface arg0, int arg1) {
  334.  
  335. }
  336. });
  337.  
  338. AlertDialog alertDialog = alertDialogBuilder.create();
  339. // show alert
  340. alertDialog.show();
  341. }
  342.  
  343. public void SelectItem(int position) {
  344.  
  345. FragmentManager fragmentManager = getSupportFragmentManager();
  346. Fragment fragment = new Fragment();
  347. int commit = 0;
  348.  
  349. switch (position) {
  350.  
  351. case 0:
  352.  
  353. styleButton.setText(getResources().getString(R.string.tests));
  354. commit = fragmentManager
  355. .beginTransaction()
  356. .replace(R.id.view_content, Test_Home.newInstance(),
  357. "FRAGMENT").addToBackStack(null).commit();
  358.  
  359. api.setSetting("name", getResources().getString(R.string.tests));
  360. break;
  361. case 1:
  362. styleButton.setText(getResources().getString(R.string.disease));
  363. commit = fragmentManager.beginTransaction()
  364. .replace(R.id.view_content, Diases_Fragment.newInstance())
  365. .addToBackStack("tag").commit();
  366. api.setSetting("name", getResources().getString(R.string.disease));
  367. break;
  368. case 2:
  369. styleButton.setText(getResources().getString(R.string.questions));
  370. commit = fragmentManager
  371. .beginTransaction()
  372. .replace(R.id.view_content,
  373. Questions_Fragment.newInstance())
  374. .addToBackStack("tag").commit();
  375. api.setSetting("name", getResources().getString(R.string.questions));
  376. break;
  377. case 3:
  378. styleButton.setText(getResources().getString(R.string.clincs));
  379. commit = fragmentManager.beginTransaction()
  380. .replace(R.id.view_content, Clinc_Fragment.newInstance())
  381. .addToBackStack("tag").commit();
  382. api.setSetting("name", getResources().getString(R.string.clincs));
  383. break;
  384. case 4:
  385. styleButton.setText(getResources().getString(R.string.medtime));
  386. commit = fragmentManager
  387. .beginTransaction()
  388. .replace(R.id.view_content, Reminder_Fragment.newInstance())
  389. .addToBackStack("tag").commit();
  390. api.setSetting("name", getResources().getString(R.string.medtime));
  391. break;
  392.  
  393. case 5:
  394. styleButton.setText(getResources().getString(R.string.articales));
  395. commit = fragmentManager
  396. .beginTransaction()
  397. .replace(R.id.view_content, ArticalesFragment.newInstance())
  398. .addToBackStack("tag").commit();
  399. api.setSetting("name", getResources().getString(R.string.articales));
  400. break;
  401. case 6:
  402. styleButton.setText(getResources().getString(R.string.profile));
  403. commit = fragmentManager.beginTransaction()
  404. .replace(R.id.view_content, Profile_Fragment.newInstance())
  405. .addToBackStack("tag").commit();
  406. api.setSetting("name", getResources().getString(R.string.profile));
  407. break;
  408. case 7:
  409. styleButton.setText(getResources().getString(R.string.compition));
  410. commit = fragmentManager
  411. .beginTransaction()
  412. .replace(R.id.view_content,
  413. Competion_Fragment.newInstance())
  414. .addToBackStack("tag").commit();
  415. api.setSetting("name", getResources().getString(R.string.compition));
  416. break;
  417. case 8:
  418.  
  419. Logout();
  420.  
  421. break;
  422.  
  423. }
  424.  
  425. DrawerList.setItemChecked(position, true);
  426. // setTitle(dataList.get(position).getItemName());
  427. drawer.closeDrawer(Gravity.RIGHT);
  428.  
  429. }
  430.  
  431. @Override
  432. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  433. super.onActivityResult(requestCode, resultCode, data);
  434.  
  435. }
  436.  
  437. public void replaceTestFragment(Test_Home fragment) {
  438. styleButton.setText(getResources().getString(R.string.tests));
  439. FragmentManager fragmentManager = getSupportFragmentManager();
  440. fragmentManager.beginTransaction()
  441. .replace(R.id.view_content, fragment.newInstance())
  442. .addToBackStack("tag").commit();
  443.  
  444.  
  445. }
  446.  
  447.  
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement