diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e479603
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+.gradle
+/local.properties
+/.idea
+.DS_Store
+/build
+/captures
+*.iml
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..d958b63
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,45 @@
+language: android
+
+jdk:
+- oraclejdk8
+
+android:
+ components:
+ - tools
+ - platform-tools
+ - tools
+ - build-tools-27.0.3
+ - android-27
+ - extra-android-support
+ - extra-google-google_play_services
+ - extra-android-m2repository
+ - extra-google-m2repository
+ - addon-google_apis-google-24
+
+before_install:
+- chmod +x gradlew
+- export JAVA8_HOME=/usr/lib/jvm/java-8-oracle
+- export JAVA_HOME=$JAVA8_HOME
+- yes | sdkmanager "platforms;android-27"
+
+after_success:
+- ./gradlew coveralls
+- chmod +x ./upload-gh-pages.sh
+- chmod +x ./import-translations-github.sh
+- ./import-translations-github.sh
+- ./upload-gh-pages.sh
+
+script:
+- cd $TRAVIS_BUILD_DIR/
+- ./gradlew testDebugUnitTest assembleDebug jacocoTestReport
+
+notifications:
+ slack: glucosio:uk2xb9sAxOaVedj7zePyuBqa
+
+before_cache:
+- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
+
+cache:
+ directories:
+ - $HOME/.gradle/caches/
+ - $HOME/.gradle/wrapper/
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
new file mode 100644
index 0000000..17e7289
--- /dev/null
+++ b/CODE_OF_CONDUCT.md
@@ -0,0 +1,15 @@
+# Contributor Code of Conduct
+
+As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
+
+We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, ability, personal appearance, body size, race, ethnicity, age, or religion.
+
+Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
+
+Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
+
+This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
+
+Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
+
+This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.1.0, available at [http://contributor-covenant.org/version/1/1/0/](http://contributor-covenant.org/version/1/1/0/)
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..99aa46b
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,48 @@
+## Submitting issues
+
+If you have questions about how to install or use Glucosio, please direct these to the [mailing list][mailinglist] or our [forum][forum]. We are also available on [Slack][slack].
+
+### Short version
+
+ * The [**issue template can be found here**][template] but be aware of the different repositories! See list below. Please always use the issue template when reporting issues.
+
+### Guidelines
+* Please search the existing issues first, it's likely that your issue was already reported or even fixed.
+ - Go to one of the repositories, click "issues" and type any word in the top search/command bar.
+ - You can also filter by appending e. g. "state:open" to the search string.
+ - More info on [search syntax within github](https://help.github.com/articles/searching-issues)
+* This repository ([glucosio-android](https://github.com/Glucosio/glucosio-android/issues)) is *only* for issues within the Glucosio for Android code.
+* __SECURITY__: Report any potential security bug to us at hello@glucosio.org instead of filing an issue in our bug tracker
+* The issues in other components should be reported in their respective repositories:
+ - [iOS client](https://github.com/Glucosio/glucosio-android/issues)
+ - [API](https://github.com/Glucosio/glucosio-api/issues)
+ - [Branding](https://github.com/Glucosio/branding-assets/issues)
+* Report the issue using our [template][template], it includes all the information we need to track down the issue.
+
+Help us to maximize the effort we can spend fixing issues and adding new features, by not reporting duplicate issues.
+
+[template]: https://github.com/Glucosio/project-tools/blob/master/gh/templates/issue_template.md
+[mailinglist]: https://groups.google.com/forum/#!forum/glucosio-help/
+[forum]: https://community.glucosio.org
+[slack]: https://slack.glucosio.org
+
+## Contributing to Source Code
+
+Thanks for wanting to contribute source code to Glucosio. That's great!
+
+Before we're able to merge your code into Glucosio, you need to sign our [Contributor Agreement][agreement].
+
+Please read the [Developer Manuals][devmanual] to learn how to work with our code base and our standards for contributing source
+code.
+
+In order to constantly increase the quality of our software we can no longer accept pull request which submit un-tested code.
+It is a must have that changed and added code segments are tested before you make a pull request.
+
+
+[agreement]: https://www.clahub.com/agreements/Glucosio/glucosio-android
+[devmanual]: https://docs.glucosio.org
+
+## Translations
+Please submit translations via [Crowdin][crowdin].
+
+[crowdin]: https://translate.glucosio.org
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..9cecc1d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ {one line to give the program's name and a brief idea of what it does.}
+ Copyright (C) {year} {name of author}
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ {project} Copyright (C) {year} {fullname}
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e69de29
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2fdef89
--- /dev/null
+++ b/README.md
@@ -0,0 +1,43 @@
+<<<<<<< HEAD
+[![Crowdin](https://d322cqt584bo4o.cloudfront.net/glucosio/localized.png)](https://crowdin.com/project/glucosio)
+[![Build Status](https://travis-ci.org/Glucosio/glucosio-android.svg)](https://travis-ci.org/Glucosio/glucosio-android)
+[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a6f7bcc22a174ac9b36795438c143b6d)](https://www.codacy.com/app/Glucosio/glucosio-android?utm_source=github.com&utm_medium=referral&utm_content=Glucosio/glucosio-android&utm_campaign=Badge_Grade)
+[![Coverage Status](https://coveralls.io/repos/github/Glucosio/glucosio-android/badge.svg?branch=develop)](https://coveralls.io/github/Glucosio/glucosio-android?branch=develop)
+
+# Glucosio has moved to Gitlab! [Visit our project repos on Gitlab](https://gitlab.com/glucosio)
+
+# Glucosio for Android
+Glucosio for Android, a user centered free and open source app for Diabetes management and research for Android.
+
+![Glucosio Banner](https://cloud.githubusercontent.com/assets/5623301/14087778/f02be08c-f52b-11e5-9ff3-15bc5670cddb.png)
+
+ https://glucosio.org
+
+## Build Instructions
+
+- Clone the project from GitHub:
+ ```
+ git clone https://github.com/Glucosio/android.git
+ ```
+ or download the .zip [here](https://github.com/Glucosio/android/archive/master.zip).
+
+- Import the project in Android Studio: **File > New > Import Project**.
+ Alternatively, from the Welcome screen, select **Import project**.
+
+Read more about contributing to Glucosio [here](http://www.glucosio.org/contribute/).
+
+## Download
+
+
+
+
+
+
+## Join Beta Testing
+Join our [Community on Google+](https://plus.google.com/communities/117048486613219870727), then [click here](https://play.google.com/apps/testing/org.glucosio.android).
+
+## Test Drive Glucosio Daily
+http://daily.glucosio.org
+=======
+# Diabetes-Monitoring
+>>>>>>> 8c561cd13531dcb791ca07b367a7e0d71df61d1d
diff --git a/app/.gitignore b/app/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/app/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/app/build.gradle b/app/build.gradle
new file mode 100644
index 0000000..312317a
--- /dev/null
+++ b/app/build.gradle
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+apply from: "${rootDir}/dependencies.gradle"
+apply plugin: 'com.android.application'
+apply plugin: 'jacoco'
+apply plugin: 'realm-android'
+
+android {
+ compileSdkVersion buildConfig.compileSdk
+
+ lintOptions {
+ abortOnError false
+ }
+
+ defaultConfig {
+ minSdkVersion buildConfig.minSdk
+ targetSdkVersion buildConfig.targetSdk
+ versionCode buildConfig.versionCode
+ versionName buildConfig.versionName
+ applicationId 'org.glucosio.android'
+
+ vectorDrawables.useSupportLibrary = true
+ testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
+ }
+
+ buildTypes {
+ debug {
+ minifyEnabled false
+ debuggable true
+
+ versionNameSuffix '-DEVEL'
+ applicationIdSuffix '.daily'
+
+ testCoverageEnabled true
+
+ buildConfigField 'String', 'GOOGLE_ANALYTICS_TRACKER', '"UA-68882401-2"'
+ buildConfigField "String[]", "TRANSLATION_ARRAY", "new String[]{}"
+ }
+
+ release {
+ debuggable false
+ minifyEnabled true
+ shrinkResources true
+ zipAlignEnabled true
+
+ buildConfigField 'String', 'GOOGLE_ANALYTICS_TRACKER', '"UA-68882401-2"'
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+ buildConfigField "String[]", "TRANSLATION_ARRAY", getAllLocales()
+ }
+ }
+
+ lintOptions {
+ abortOnError false
+ }
+
+ testOptions {
+ unitTests.returnDefaultValues = true
+
+ unitTests.all {
+ jacoco {
+ includeNoLocationClasses = true
+ }
+ // configure the test JVM arguments
+ jvmArgs '-noverify'
+ }
+
+ unitTests {
+ includeAndroidResources = true
+ }
+ }
+}
+
+repositories {
+ mavenCentral()
+ jcenter()
+ maven { url 'https://jitpack.io' }
+}
+
+dependencies {
+ wearApp project(':wear')
+
+ // Android Support libraries
+ implementation deps.android.support.wearable
+
+ implementation deps.android.support.design
+ implementation deps.android.support.cardView
+ implementation deps.android.support.recyclerView
+ implementation deps.android.support.percent
+
+ // Google Play Services
+ implementation deps.android.gms.analytics
+ implementation deps.android.gms.drive
+ implementation deps.android.gms.gcm
+ implementation deps.android.gms.wearable
+
+ // Firebase
+ implementation deps.google.firebase.crash
+ implementation deps.google.firebase.invites
+ implementation deps.google.firebase.messaging
+
+ // Other libraries
+ implementation deps.ChrisJenx.calligraphy
+ implementation deps.DanLew.androidJoda
+ implementation(deps.MikePenz.materialDrawer) {
+ transitive = true
+ }
+ implementation deps.PaoloRotolo.expandableHeightListView
+ implementation deps.PhilJay.mpAndroidChart
+
+ // Butterknife
+ implementation deps.JakeWharton.butterKnife.core
+ annotationProcessor deps.JakeWharton.butterKnife.compiler
+
+ // Tests
+ testImplementation deps.junit
+ testImplementation deps.mockito
+ testImplementation deps.square.assertJAndroid
+ testImplementation deps.robolectric.core
+ testImplementation deps.robolectric.shadows
+
+ // This is for Mockito and Realm (TODO: find clean solution)
+ testImplementation 'io.reactivex.rxjava2:rxjava:2.1.14'
+
+ // Instrumental Tests
+ androidTestImplementation deps.android.test.runner
+ androidTestImplementation deps.android.test.rules
+ androidTestImplementation deps.android.test.espresso.core
+ androidTestImplementation deps.android.test.espresso.intents
+ androidTestImplementation deps.android.test.espresso.contrib
+}
+
+def getAllLocales() {
+ def foundLocales = new StringBuilder()
+ foundLocales.append("new String[]{")
+
+ fileTree("src/main/res").visit { FileVisitDetails details ->
+ if (details.file.path.endsWith("strings.xml")) {
+ def languageCode = details.file.parent.tokenize('/\\').last().replaceAll('values-', '').replaceAll('-r', '-')
+ languageCode = (languageCode == "values") ? "en" : languageCode
+ foundLocales.append("\"").append(languageCode).append("\"").append(",")
+ }
+ }
+
+ foundLocales.append("}")
+ //Don't forget to remove the trailing comma
+ return foundLocales.toString().replaceAll(',}', '}')
+}
+
+apply plugin: 'com.google.gms.google-services'
+
+def coverageSourceDirs = ['src/main/java']
+
+task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
+ group = 'Reporting'
+
+ description = 'Generate Jacoco coverage reports'
+
+ classDirectories = fileTree(dir: 'build/intermediates/classes/debug',
+ excludes: ['**/R.class',
+ '**/R$*.class',
+ '**/*$ViewBinder*.*',
+ '**/*$InjectAdapter.*',
+ '**/BuildConfig.*',
+ '**/Manifest*.*',
+ 'io/realm/**/*'])
+
+ additionalSourceDirs = files(coverageSourceDirs)
+ sourceDirectories = files(coverageSourceDirs)
+ executionData = files('build/jacoco/testDebugUnitTest.exec')
+
+ reports {
+ xml.enabled = true
+ html.enabled = true
+ csv.enabled = false
+ }
+}
+
+apply plugin: 'com.github.kt3k.coveralls'
+
+coveralls {
+ sourceDirs = files(['src/main/java']).files.absolutePath
+ jacocoReportPath = 'build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml'
+}
+
+// run: gradlew -PwithDexcount assembleDebug
+def dexCount = project.hasProperty('withDexcount')
+if (dexCount) {
+ apply plugin: 'com.getkeepsafe.dexcount'
+
+ dexcount {
+ includeTotalMethodCount = true
+ }
+}
diff --git a/app/google-services.json b/app/google-services.json
new file mode 100644
index 0000000..bcffbbe
--- /dev/null
+++ b/app/google-services.json
@@ -0,0 +1,118 @@
+{
+ "project_info": {
+ "project_number": "45702392997",
+ "firebase_url": "https://glucosio-15798.firebaseio.com",
+ "project_id": "glucosio-15798",
+ "storage_bucket": "glucosio-15798.appspot.com"
+ },
+ "client": [
+ {
+ "client_info": {
+ "mobilesdk_app_id": "1:45702392997:android:f3028c79631bec51",
+ "android_client_info": {
+ "package_name": "org.glucosio.android"
+ }
+ },
+ "oauth_client": [
+ {
+ "client_id": "45702392997-ueime3quq29n9r7ir2o87n2jinvuggg3.apps.googleusercontent.com",
+ "client_type": 1,
+ "android_info": {
+ "package_name": "org.glucosio.android",
+ "certificate_hash": "A18AA65A6EFE7F0DE3BA409A02EA6703E21CFEFD"
+ }
+ },
+ {
+ "client_id": "45702392997-8gb1fse9kp6leslqp2jil5hfqs5ls033.apps.googleusercontent.com",
+ "client_type": 3
+ },
+ {
+ "client_id": "45702392997-75lumfd1jtpvd7n2q7sac4ebrsjfcbp6.apps.googleusercontent.com",
+ "client_type": 3
+ }
+ ],
+ "api_key": [
+ {
+ "current_key": "AIzaSyCXgRfFDiHPOB0fI8tJXWavDb3y51urRsI"
+ }
+ ],
+ "services": {
+ "analytics_service": {
+ "status": 2,
+ "analytics_property": {
+ "tracking_id": "UA-68882401-2"
+ }
+ },
+ "appinvite_service": {
+ "status": 2,
+ "other_platform_oauth_client": [
+ {
+ "client_id": "45702392997-8gb1fse9kp6leslqp2jil5hfqs5ls033.apps.googleusercontent.com",
+ "client_type": 3
+ }
+ ]
+ },
+ "ads_service": {
+ "status": 1
+ }
+ }
+ },
+ {
+ "client_info": {
+ "mobilesdk_app_id": "1:45702392997:android:e595132614f4ec2a",
+ "android_client_info": {
+ "package_name": "org.glucosio.android.daily"
+ }
+ },
+ "oauth_client": [
+ {
+ "client_id": "45702392997-riuobp0po7qf98ktcdcva39s2bhbpk49.apps.googleusercontent.com",
+ "client_type": 1,
+ "android_info": {
+ "package_name": "org.glucosio.android.daily",
+ "certificate_hash": "AB3309F7EB2D25BF7B007C54AF59A3D096F1C50E"
+ }
+ },
+ {
+ "client_id": "45702392997-le0hot86nfci96j5dn6lnlnahn6lg80e.apps.googleusercontent.com",
+ "client_type": 1,
+ "android_info": {
+ "package_name": "org.glucosio.android.daily",
+ "certificate_hash": "9DF65CBC6293EB82523180CAF03912C6CC301FBD"
+ }
+ },
+ {
+ "client_id": "45702392997-8gb1fse9kp6leslqp2jil5hfqs5ls033.apps.googleusercontent.com",
+ "client_type": 3
+ },
+ {
+ "client_id": "45702392997-75lumfd1jtpvd7n2q7sac4ebrsjfcbp6.apps.googleusercontent.com",
+ "client_type": 3
+ }
+ ],
+ "api_key": [
+ {
+ "current_key": "AIzaSyAWcm8BiItil81kEEl3t3zwKnUvyR6yvJ4"
+ }
+ ],
+ "services": {
+ "analytics_service": {
+ "status": 1
+ },
+ "appinvite_service": {
+ "status": 2,
+ "other_platform_oauth_client": [
+ {
+ "client_id": "45702392997-8gb1fse9kp6leslqp2jil5hfqs5ls033.apps.googleusercontent.com",
+ "client_type": 3
+ }
+ ]
+ },
+ "ads_service": {
+ "status": 1
+ }
+ }
+ }
+ ],
+ "configuration_version": "1"
+}
\ No newline at end of file
diff --git a/app/libs/commons-math3-3.6.1.jar b/app/libs/commons-math3-3.6.1.jar
new file mode 100644
index 0000000..0ff582c
Binary files /dev/null and b/app/libs/commons-math3-3.6.1.jar differ
diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro
new file mode 100644
index 0000000..5c6f0d3
--- /dev/null
+++ b/app/proguard-rules.pro
@@ -0,0 +1,115 @@
+-optimizationpasses 5
+-dump class_files.txt
+-printseeds seeds.txt
+-printusage unused.txt
+-printmapping mapping.txt
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging*/
+-allowaccessmodification
+-repackageclasses ''
+
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.MapActivity
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+
+-keep public class org.apache.commons.io.**
+-keep public class com.google.gson.**
+-keep public class com.google.gson.** {public private protected *;}
+
+##---------------Begin: proguard configuration for Gson ----------
+-keepattributes *Annotation*,Signature
+-keep class org.glucosio.android.ActivityMonitor.ClassMultiPoints.** { *; }
+-keep public class org.glucosio.android.ActivityMonitor$ClassMultiPoints { public protected *; }
+-keep public class org.glucosio.android.ActivityMonitor$ClassMultiPoints$ClassPoints { public protected *; }
+-keep public class org.glucosio.android.ActivityMonitor$ClassMultiPoints$ClassPoints$ClassPoint { public protected *; }
+-keepclassmembers enum * { *; }
+
+##---------------End: proguard configuration for Gson ----------
+
+
+
+# MPAndoridChart
+-keep class com.github.mikephil.charting.** { *; }
+
+# RxAndroid
+-dontwarn rx.internal.util.unsafe.**
+
+# Realm
+-keep class io.realm.annotations.RealmModule
+-keep @io.realm.annotations.RealmModule class *
+-keep class io.realm.internal.Keep
+-keep @io.realm.internal.Keep class * { *; }
+-dontwarn javax.**
+-dontwarn io.realm.**
+
+## AppCompat
+-keep public class android.support.v7.widget.** { *; }
+-keep public class android.support.v7.internal.widget.** { *; }
+-keep public class android.support.v7.internal.view.menu.** { *; }
+
+-keep public class * extends android.support.v4.view.ActionProvider {
+ public (android.content.Context);
+}
+
+## Google Play Services
+-dontnote com.google.vending.licensing.ILicensingService
+-keep class * extends java.util.ListResourceBundle {
+ protected Object[][] getContents();
+}
+
+-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
+ public static final *** NULL;
+}
+
+-keepnames @com.google.android.gms.common.annotation.KeepName class *
+-keepclassmembernames class * {
+ @com.google.android.gms.common.annotation.KeepName *;
+}
+
+-keepnames class * implements android.os.Parcelable {
+ public static final ** CREATOR;
+}
+
+## Instabug
+-dontwarn org.apache.http.**
+-dontwarn android.net.http.AndroidHttpClient
+-dontwarn com.google.android.gms.**
+-dontwarn com.android.volley.toolbox.**
+-dontwarn com.instabug.**
+
+## Smooch
+-dontwarn okio.**
+-keep class okio.**
+-keep class com.google.gson.**
+
+
+-keep class * extends java.util.ListResourceBundle {
+ protected java.lang.Object[][] getContents();
+}
+
+# Keep SafeParcelable value, needed for reflection. This is required to support backwards
+# compatibility of some classes.
+-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
+ public static final *** NULL;
+}
+
+# Keep the names of classes/members we need for client functionality.
+-keepnames @com.google.android.gms.common.annotation.KeepName class *
+-keepclassmembernames class * {
+ @com.google.android.gms.common.annotation.KeepName *;
+}
+
+# Needed for Parcelable/SafeParcelable Creators to not get stripped
+-keepnames class * implements android.os.Parcelable {
+ public static final ** CREATOR;
+}
+
+# Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
+
+-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
+
+-keepclassmembers class * {
+ @com.google.api.client.util.Key ;
+}
\ No newline at end of file
diff --git a/app/release/app-release.apk b/app/release/app-release.apk
new file mode 100644
index 0000000..cbd781b
Binary files /dev/null and b/app/release/app-release.apk differ
diff --git a/app/release/output.json b/app/release/output.json
new file mode 100644
index 0000000..35a5e9c
--- /dev/null
+++ b/app/release/output.json
@@ -0,0 +1,19 @@
+[
+ {
+ "outputType": {
+ "type": "APK"
+ },
+ "apkInfo": {
+ "type": "MAIN",
+ "splits": [],
+ "versionCode": 43,
+ "versionName": "1.5.0",
+ "enabled": true,
+ "outputFile": "app-release.apk",
+ "fullName": "release",
+ "baseName": "release"
+ },
+ "path": "app-release.apk",
+ "properties": {}
+ }
+]
\ No newline at end of file
diff --git a/app/release/wear-release.apk b/app/release/wear-release.apk
new file mode 100644
index 0000000..75b4fe3
Binary files /dev/null and b/app/release/wear-release.apk differ
diff --git a/app/src/androidTest/java/org/glucosio/android/HelloActivityTest.java b/app/src/androidTest/java/org/glucosio/android/HelloActivityTest.java
new file mode 100644
index 0000000..5cb387a
--- /dev/null
+++ b/app/src/androidTest/java/org/glucosio/android/HelloActivityTest.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android;
+
+import android.support.test.espresso.ViewInteraction;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.glucosio.android.activity.HelloActivity;
+import org.glucosio.android.util.CustomClickAction;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.MethodSorters;
+
+import static android.support.test.espresso.Espresso.onData;
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.action.ViewActions.click;
+import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
+import static android.support.test.espresso.action.ViewActions.scrollTo;
+import static android.support.test.espresso.action.ViewActions.typeText;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
+import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withId;
+import static android.support.test.espresso.matcher.ViewMatchers.withParent;
+import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * @author amouly on 11/11/15.
+ */
+
+@RunWith(AndroidJUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class HelloActivityTest {
+
+ private static final String STRING_TYPED_AGE = "23";
+ private static final String VALID_COUNTRY = "Argentina";
+ private static final String VALID_GENDER = "Other";
+ private static final String VALID_TYPE = "Type 2";
+
+ @Rule
+ public ActivityTestRule mRule = new ActivityTestRule<>(HelloActivity.class);
+ private int[] helloActivityViews = {
+ R.id.activity_hello_title,
+ R.id.activity_hello_subtitle,
+ R.id.activity_hello_spinner_country,
+ R.id.activity_hello_age,
+ R.id.activity_hello_spinner_gender,
+ R.id.activity_hello_spinner_diabetes_type,
+ R.id.activity_hello_spinner_preferred_unit,
+ R.id.activity_hello_check_share};
+
+ @BeforeClass
+ public static void clearAppData() {
+ try {
+ // clearing app data
+ Runtime runtime = Runtime.getRuntime();
+ runtime.exec("pm clear org.glucosio.android");
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void check_001_IfHelloActivityIsCompletelyDisplayed() {
+ for (int id : helloActivityViews) {
+ if (id == R.id.activity_hello_check_share) {
+ ViewInteraction checkButtonInteraction = onView(withId(id)).perform(scrollTo());
+ checkButtonInteraction.check(matches(isDisplayed()));
+ continue;
+ }
+ onView(withId(id)).check(matches(isDisplayed()));
+ }
+ }
+
+ @Test
+ public void check_002_IfStartButtonIsDisplayed() {
+ onView(withId(R.id.activity_hello_button_start))
+ .perform(scrollTo())
+ .check(matches(isDisplayed()));
+ }
+
+ @Test
+ public void check_003_IfICanUseCountrySpinnerToSelectMyActualCountry() {
+ // Click on Country spinner
+ onView(withId(R.id.activity_hello_spinner_country)).perform(click());
+
+ // Select random Country
+ onData(allOf(is(instanceOf(String.class)), is(VALID_COUNTRY))).perform(click());
+
+ // Locate Spinner view and check its text is equal with COUNTRY
+ onView(allOf(withId(R.id.custom_spinner),
+ withParent(withId(R.id.activity_hello_spinner_country))))
+ .check(matches(withSpinnerText(VALID_COUNTRY)));
+ }
+
+ @Test
+ public void check_004_IfICanEnterMyAgeUsingHelloAgeEditText() {
+ // Enter a valid Age
+ onView(withId(R.id.activity_hello_age))
+ .perform(typeText(STRING_TYPED_AGE), closeSoftKeyboard());
+ }
+
+ @Test
+ public void check_005_IfCanUseGenderSpinnerToSelectMyGender() {
+ // Click on Gender spinner
+ onView(withId(R.id.activity_hello_spinner_gender))
+ .perform(CustomClickAction.click());
+
+ // Select random Gender
+ onData(allOf(is(instanceOf(String.class)), is(VALID_GENDER)))
+ .perform(CustomClickAction.click());
+
+ // Locate Spinner view and check its text is equal with VALID_GENDER
+ onView(allOf(withId(R.id.custom_spinner),
+ withParent(withId(R.id.activity_hello_spinner_gender))))
+ .check(matches(withSpinnerText(VALID_GENDER)));
+ }
+
+ @Test
+ public void check_006_IfICanUseTypeSpinnerToChangeDiabetesType() {
+ // Click on Type spinner
+ onView(withId(R.id.activity_hello_spinner_diabetes_type))
+ .perform(CustomClickAction.click());
+
+ // Select random Type
+ onData(allOf(is(instanceOf(String.class)), is(VALID_TYPE)))
+ .perform(CustomClickAction.click());
+
+ // Locate Spinner view and check its text is equal with VALID_TYPE
+ onView(allOf(withId(R.id.custom_spinner),
+ withParent(withId(R.id.activity_hello_spinner_diabetes_type))))
+ .check(matches(withSpinnerText(VALID_TYPE)));
+ }
+
+ @Test
+ public void check_007_IfICanUseUnitSpinnerToSelectPreferredUnit() {
+ // Click on Unit spinner
+ onView(withId(R.id.activity_hello_spinner_preferred_unit))
+ .perform(CustomClickAction.click());
+
+ // Select random Unit
+ onData(allOf(is(instanceOf(String.class)), is(Constants.Units.MG_DL)))
+ .perform(CustomClickAction.click());
+
+ // Locate Spinner view and check its text is equal with VALID_UNIT
+ onView(allOf(withId(R.id.custom_spinner),
+ withParent(withId(R.id.activity_hello_spinner_preferred_unit))))
+ .check(matches(withSpinnerText(Constants.Units.MG_DL)));
+ }
+
+ @Test
+ public void check_008_IfICanUncheckShareDataCheckBox() {
+ // Click on Share Data CheckBox multiple times
+ onView(withId(R.id.activity_hello_check_share))
+ .check(matches(isChecked()))
+ .perform(scrollTo(), click()) // this checkButton needs to be scrolled to
+ .check(matches(not(isChecked())));
+ }
+
+ @Test
+ public void check_009_IfICanSubmitAnyData() {
+ // Perform submit
+ onView(withId(R.id.activity_hello_button_start))
+ .perform(scrollTo(), click());
+ }
+
+ @Test
+ public void check_010_IfIEnterWrongAge() {
+ // Entering invalid age
+ onView(withId(R.id.activity_hello_age))
+ .perform(typeText("0"), closeSoftKeyboard());
+
+ // Click on GET STARTED button
+ onView(withId(R.id.activity_hello_button_start))
+ .perform(scrollTo(), click());
+
+ // Checking the toast is displayed with invalid message
+ onView(withText(R.string.helloactivity_age_invalid))
+ .inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
+ .check(matches(isDisplayed()));
+ }
+}
diff --git a/app/src/androidTest/java/org/glucosio/android/MainActivityTest.java b/app/src/androidTest/java/org/glucosio/android/MainActivityTest.java
new file mode 100644
index 0000000..2d09544
--- /dev/null
+++ b/app/src/androidTest/java/org/glucosio/android/MainActivityTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android;
+
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.glucosio.android.activity.MainActivity;
+import org.junit.FixMethodOrder;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.MethodSorters;
+
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withId;
+
+/**
+ * @author piotr on 29/10/15.
+ */
+@RunWith(AndroidJUnit4.class)
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class MainActivityTest {
+
+ @Rule
+ public ActivityTestRule mRule = new ActivityTestRule<>(MainActivity.class);
+
+ private HelloActivityTest previousTest = new HelloActivityTest();
+
+ private void goThroughHelloActivity() {
+ previousTest.check_004_IfICanEnterMyAgeUsingHelloAgeEditText();
+ previousTest.check_009_IfICanSubmitAnyData();
+ }
+
+ @Test
+ public void check_001_checkIfToolbarIsDisplayed() throws InterruptedException {
+ goThroughHelloActivity();
+ onView(withId(R.id.activity_main_toolbar)).check(matches(isDisplayed()));
+ }
+
+ // TODO: 09/09/16 Test the responses in UI -> Show ui notice when export started, is empty or error
+}
\ No newline at end of file
diff --git a/app/src/androidTest/java/org/glucosio/android/util/CustomClickAction.java b/app/src/androidTest/java/org/glucosio/android/util/CustomClickAction.java
new file mode 100644
index 0000000..72b6683
--- /dev/null
+++ b/app/src/androidTest/java/org/glucosio/android/util/CustomClickAction.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.util;
+
+/**
+ * @author amouly on 11/12/15.
+ */
+
+import android.support.test.espresso.PerformException;
+import android.support.test.espresso.UiController;
+import android.support.test.espresso.ViewAction;
+import android.support.test.espresso.action.CoordinatesProvider;
+import android.support.test.espresso.action.GeneralLocation;
+import android.support.test.espresso.action.PrecisionDescriber;
+import android.support.test.espresso.action.Press;
+import android.support.test.espresso.action.Tap;
+import android.support.test.espresso.action.Tapper;
+import android.support.test.espresso.core.internal.deps.guava.base.Optional;
+import android.support.test.espresso.util.HumanReadables;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.webkit.WebView;
+
+import org.hamcrest.Matcher;
+
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast;
+import static org.hamcrest.Matchers.allOf;
+
+/**
+ * Enables clicking on views.
+ */
+public final class CustomClickAction implements ViewAction {
+ private final CoordinatesProvider coordinatesProvider;
+ private final Tapper tapper;
+ private final PrecisionDescriber precisionDescriber;
+ private final Optional rollbackAction;
+
+ public CustomClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider,
+ PrecisionDescriber precisionDescriber) {
+ this(tapper, coordinatesProvider, precisionDescriber, null);
+ }
+
+ public CustomClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider,
+ PrecisionDescriber precisionDescriber, ViewAction rollbackAction) {
+ this.coordinatesProvider = coordinatesProvider;
+ this.tapper = tapper;
+ this.precisionDescriber = precisionDescriber;
+ this.rollbackAction = Optional.fromNullable(rollbackAction);
+ }
+
+ public static ViewAction click() {
+ return new CustomClickAction(Tap.SINGLE, GeneralLocation.CENTER, Press.FINGER);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Matcher getConstraints() {
+ Matcher standardConstraint = isDisplayingAtLeast(10);
+ if (rollbackAction.isPresent()) {
+ return allOf(standardConstraint, rollbackAction.get().getConstraints());
+ } else {
+ return standardConstraint;
+ }
+ }
+
+ @Override
+ public void perform(UiController uiController, View view) {
+ float[] coordinates = coordinatesProvider.calculateCoordinates(view);
+ float[] precision = precisionDescriber.describePrecision();
+ Tapper.Status status = Tapper.Status.FAILURE;
+ int loopCount = 0;
+ // Native event injection is quite a tricky process. A tap is actually 2
+ // seperate motion events which need to get injected into the system. Injection
+ // makes an RPC call from our app under test to the Android system server, the
+ // system server decides which window layer to deliver the event to, the system
+ // server makes an RPC to that window layer, that window layer delivers the event
+ // to the correct UI element, activity, or window object. Now we need to repeat
+ // that 2x. for a simple down and up. Oh and the down event triggers timers to
+ // detect whether or not the event is a long vs. short press. The timers are
+ // removed the moment the up event is received (NOTE: the possibility of eventTime
+ // being in the future is totally ignored by most motion event processors).
+ //
+ // Phew.
+ //
+ // The net result of this is sometimes we'll want to do a regular tap, and for
+ // whatever reason the up event (last half) of the tap is delivered after long
+ // press timeout (depending on system load) and the long press behaviour is
+ // displayed (EG: show a context menu). There is no way to avoid or handle this more
+ // gracefully. Also the longpress behavour is app/widget specific. So if you have
+ // a seperate long press behaviour from your short press, you can pass in a
+ // 'RollBack' ViewAction which when executed will undo the effects of long press.
+ while (status != Tapper.Status.SUCCESS && loopCount < 3) {
+ try {
+ status = tapper.sendTap(uiController, coordinates, precision);
+ } catch (RuntimeException re) {
+ throw new PerformException.Builder()
+ .withActionDescription(this.getDescription())
+ .withViewDescription(HumanReadables.describe(view))
+ .withCause(re)
+ .build();
+ }
+ int duration = ViewConfiguration.getPressedStateDuration();
+ // ensures that all work enqueued to process the tap has been run.
+ if (duration > 0) {
+ uiController.loopMainThreadForAtLeast(duration);
+ }
+ if (status == Tapper.Status.WARNING) {
+ if (rollbackAction.isPresent()) {
+ rollbackAction.get().perform(uiController, view);
+ } else {
+ break;
+ }
+ }
+ loopCount++;
+ }
+ if (status == Tapper.Status.FAILURE) {
+ throw new PerformException.Builder()
+ .withActionDescription(this.getDescription())
+ .withViewDescription(HumanReadables.describe(view))
+ .withCause(new RuntimeException(String.format("Couldn't "
+ + "click at: %s,%s precision: %s, %s . Tapper: %s coordinate provider: %s precision " +
+ "describer: %s. Tried %s times. With Rollback? %s", coordinates[0], coordinates[1],
+ precision[0], precision[1], tapper, coordinatesProvider, precisionDescriber, loopCount,
+ rollbackAction.isPresent())))
+ .build();
+ }
+ if (tapper == Tap.SINGLE && view instanceof WebView) {
+ // WebViews will not process click events until double tap
+ // timeout. Not the best place for this - but good for now.
+ uiController.loopMainThreadForAtLeast(ViewConfiguration.getDoubleTapTimeout());
+ }
+ }
+
+ @Override
+ public String getDescription() {
+ return tapper.toString().toLowerCase() + " click";
+ }
+}
diff --git a/app/src/debug/res/mipmap-hdpi/ic_launcher.png b/app/src/debug/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..2922401
Binary files /dev/null and b/app/src/debug/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/app/src/debug/res/mipmap-mdpi/ic_launcher.png b/app/src/debug/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..bd6634a
Binary files /dev/null and b/app/src/debug/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/app/src/debug/res/mipmap-xhdpi/ic_launcher.png b/app/src/debug/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..16d695a
Binary files /dev/null and b/app/src/debug/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png b/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..7b933d0
Binary files /dev/null and b/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..eb803ac
Binary files /dev/null and b/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/app/src/debug/res/values/strings.xml b/app/src/debug/res/values/strings.xml
new file mode 100644
index 0000000..25db463
--- /dev/null
+++ b/app/src/debug/res/values/strings.xml
@@ -0,0 +1,23 @@
+
+
+
+ Glucosio Daily
+
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..fbec94f
--- /dev/null
+++ b/app/src/main/AndroidManifest.xml
@@ -0,0 +1,265 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/assets/fonts/lato-bold.ttf b/app/src/main/assets/fonts/lato-bold.ttf
new file mode 100644
index 0000000..7434369
Binary files /dev/null and b/app/src/main/assets/fonts/lato-bold.ttf differ
diff --git a/app/src/main/assets/fonts/lato-light.ttf b/app/src/main/assets/fonts/lato-light.ttf
new file mode 100644
index 0000000..a958067
Binary files /dev/null and b/app/src/main/assets/fonts/lato-light.ttf differ
diff --git a/app/src/main/assets/fonts/lato.ttf b/app/src/main/assets/fonts/lato.ttf
new file mode 100644
index 0000000..04ea8ef
Binary files /dev/null and b/app/src/main/assets/fonts/lato.ttf differ
diff --git a/app/src/main/assets/fonts/opendyslexic-bold.otf b/app/src/main/assets/fonts/opendyslexic-bold.otf
new file mode 100644
index 0000000..4c492e2
Binary files /dev/null and b/app/src/main/assets/fonts/opendyslexic-bold.otf differ
diff --git a/app/src/main/assets/fonts/opendyslexic.otf b/app/src/main/assets/fonts/opendyslexic.otf
new file mode 100644
index 0000000..1a7c9d4
Binary files /dev/null and b/app/src/main/assets/fonts/opendyslexic.otf differ
diff --git a/app/src/main/java/org/glucosio/android/Constants.java b/app/src/main/java/org/glucosio/android/Constants.java
new file mode 100644
index 0000000..0febc61
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/Constants.java
@@ -0,0 +1,14 @@
+package org.glucosio.android;
+
+public class Constants {
+ private Constants() {
+ }
+
+ public class Units {
+ public static final String MG_DL = "mg/dL";
+ public static final String MMOL_L = "mmol/L";
+
+ private Units() {
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/GlucosioApplication.java b/app/src/main/java/org/glucosio/android/GlucosioApplication.java
new file mode 100644
index 0000000..08f3f5f
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/GlucosioApplication.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android;
+
+import android.app.Application;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.VisibleForTesting;
+
+import org.glucosio.android.activity.A1cCalculatorActivity;
+import org.glucosio.android.activity.HelloActivity;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.analytics.GlucosioGoogleAnalytics;
+import org.glucosio.android.backup.Backup;
+import org.glucosio.android.backup.GoogleDriveBackup;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.User;
+import org.glucosio.android.presenter.A1CCalculatorPresenter;
+import org.glucosio.android.presenter.HelloPresenter;
+import org.glucosio.android.tools.LocaleHelper;
+import org.glucosio.android.tools.Preferences;
+
+import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
+
+public class GlucosioApplication extends Application {
+
+ private static GlucosioApplication sInstance;
+
+ @Nullable
+ private Analytics analytics;
+
+ @Nullable
+ private LocaleHelper localeHelper;
+
+ @Nullable
+ private Preferences preferences;
+
+ public static GlucosioApplication getInstance() {
+ if (sInstance == null) {
+ sInstance = new GlucosioApplication();
+ }
+ return sInstance;
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ sInstance = this;
+ initFont();
+ initLanguage();
+ }
+
+ @VisibleForTesting
+ protected void initFont() {
+ //TODO: convert of using new introduced class Preferences
+ // Get Dyslexia preference and adjust font
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
+ boolean isDyslexicModeOn = sharedPref.getBoolean("pref_font_dyslexia", false);
+
+ if (isDyslexicModeOn) {
+ setFont("fonts/opendyslexic.otf");
+ } else {
+ setFont("fonts/lato.ttf");
+ }
+ }
+
+ @VisibleForTesting
+ protected void initLanguage() {
+ User user = getDBHandler().getUser(1);
+ if (user != null) {
+ checkBadLocale(user);
+
+ String languageTag = user.getPreferred_language();
+ if (languageTag != null) {
+ getLocaleHelper().updateLanguage(this, languageTag);
+ }
+ }
+ }
+
+ private void checkBadLocale(User user) {
+ Preferences preferences = getPreferences();
+ boolean cleanLocaleDone = preferences.isLocaleCleaned();
+
+ if (!cleanLocaleDone) {
+ User updatedUser = new User(user);
+ updatedUser.setPreferred_language(null);
+ //TODO: is it long operation? should we move it to separate thread?
+ getDBHandler().updateUser(updatedUser);
+ preferences.saveLocaleCleaned();
+ }
+ }
+
+ private void setFont(String font) {
+ CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
+ .setDefaultFontPath(font)
+ .setFontAttrId(R.attr.fontPath)
+ .build());
+ }
+
+ @NonNull
+ public Backup getBackup() {
+ return new GoogleDriveBackup();
+ }
+
+ @NonNull
+ public Analytics getAnalytics() {
+ if (analytics == null) {
+ analytics = new GlucosioGoogleAnalytics();
+ analytics.init(this);
+ }
+
+ return analytics;
+ }
+
+ @NonNull
+ public DatabaseHandler getDBHandler() {
+ return new DatabaseHandler(getApplicationContext());
+ }
+
+ @NonNull
+ public A1CCalculatorPresenter createA1cCalculatorPresenter(@NonNull final A1cCalculatorActivity activity) {
+ return new A1CCalculatorPresenter(activity, getDBHandler());
+ }
+
+ @NonNull
+ public LocaleHelper getLocaleHelper() {
+ if (localeHelper == null) {
+ localeHelper = new LocaleHelper();
+ }
+ return localeHelper;
+ }
+
+ @NonNull
+ public Preferences getPreferences() {
+ if (preferences == null) {
+ preferences = new Preferences(this);
+ }
+
+ return preferences;
+ }
+
+ @NonNull
+ public HelloPresenter createHelloPresenter(@NonNull final HelloActivity activity) {
+ return new HelloPresenter(activity, getDBHandler());
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/A1cCalculatorActivity.java b/app/src/main/java/org/glucosio/android/activity/A1cCalculatorActivity.java
new file mode 100644
index 0000000..51473f7
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/A1cCalculatorActivity.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.text.Editable;
+import android.view.KeyEvent;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.TextView;
+
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.presenter.A1CCalculatorPresenter;
+
+import java.text.NumberFormat;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import butterknife.OnEditorAction;
+import butterknife.OnTextChanged;
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public class A1cCalculatorActivity extends AppCompatActivity {
+
+ @BindView(R.id.activity_converter_a1c_glucose_unit)
+ TextView glucoseUnit;
+
+ @BindView(R.id.activity_converter_a1c_a1c)
+ TextView A1CTextView;
+
+ @BindView(R.id.activity_converter_a1c_a1c_unit)
+ TextView A1cUnitTextView;
+
+ private double convertedA1C = 0;
+ private A1CCalculatorPresenter presenter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.activity_a1_calculator);
+ ButterKnife.bind(this);
+
+ initActionBar();
+
+ GlucosioApplication application = (GlucosioApplication) getApplication();
+ presenter = application.createA1cCalculatorPresenter(this);
+
+ if (!"percentage".equals(presenter.getA1cUnit())) {
+ A1cUnitTextView.setText(getString(R.string.mmol_mol));
+ }
+
+ presenter.checkGlucoseUnit();
+ }
+
+ @OnTextChanged(value = R.id.activity_converter_a1c_glucose, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
+ void glucoseValueChanged(@NonNull final Editable s) {
+ convertedA1C = presenter.calculateA1C(s.toString());
+ A1CTextView.setText(String.valueOf(NumberFormat.getInstance().format(convertedA1C)));
+ }
+
+ @SuppressWarnings("UnusedParameters")
+ @OnEditorAction(R.id.activity_converter_a1c_glucose)
+ boolean editorAction(TextView view, int actionId, KeyEvent event) {
+ return actionId == EditorInfo.IME_ACTION_DONE;
+ }
+
+ private void initActionBar() {
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ //noinspection ConstantConditions
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+ }
+
+ public void setMmol() {
+ glucoseUnit.setText(getString(R.string.mmol_L));
+ }
+
+ @Override
+ public boolean onKeyUp(int keyCode, KeyEvent event) {
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
+ ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
+ (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
+ }
+ return super.onKeyUp(keyCode, event);
+ }
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
+ ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
+ (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
+ }
+ return super.onKeyDown(keyCode, event);
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.menu_converter_a1c, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.action_menu_save:
+ presenter.saveA1C(convertedA1C);
+ break;
+ case android.R.id.home:
+ finish();
+ break;
+ default:
+ return super.onOptionsItemSelected(item);
+ }
+ return true;
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AboutActivity.java b/app/src/main/java/org/glucosio/android/activity/AboutActivity.java
new file mode 100644
index 0000000..211bed1
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AboutActivity.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceFragment;
+import android.support.v7.app.AppCompatActivity;
+import android.view.MenuItem;
+import android.widget.Toast;
+
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.tools.network.GlucosioExternalLinks;
+
+import java.util.Locale;
+
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public class AboutActivity extends AppCompatActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.preferences_about);
+
+ getFragmentManager().beginTransaction()
+ .replace(R.id.aboutPreferencesFrame, new MyPreferenceFragment()).commit();
+
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setTitle(getString(R.string.preferences_about_glucosio));
+ }
+
+ public boolean onOptionsItemSelected(MenuItem item) {
+ finish();
+ return true;
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+
+ public static class MyPreferenceFragment extends PreferenceFragment {
+
+ @Override
+ public void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.about_preference);
+
+ final Preference licencesPref = findPreference("preference_licences");
+ final Preference ratePref = findPreference("preference_rate");
+ final Preference feedbackPref = findPreference("preference_feedback");
+ final Preference privacyPref = findPreference("preference_privacy");
+ final Preference termsPref = findPreference("preference_terms");
+ final Preference versionPref = findPreference("preference_version");
+ final Preference thanksPref = findPreference("preference_thanks");
+
+
+ termsPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ ExternalLinkActivity.launch(
+ getActivity(),
+ getString(R.string.preferences_terms),
+ GlucosioExternalLinks.TERMS);
+ addTermsAnalyticsEvent("Glucosio Terms opened");
+ return false;
+ }
+ });
+
+ licencesPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ ExternalLinkActivity.launch(
+ getActivity(),
+ getString(R.string.preferences_licences_open),
+ GlucosioExternalLinks.LICENSES);
+ addTermsAnalyticsEvent("Glucosio Licence opened");
+ return false;
+ }
+ });
+
+ ratePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=org.glucosio.android"));
+ startActivity(intent);
+
+ return false;
+ }
+ });
+
+ feedbackPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ // Open email intent
+ Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:hello@glucosio.org"));
+ boolean activityExists = emailIntent.resolveActivityInfo(getActivity().getPackageManager(), 0) != null;
+
+ if (activityExists) {
+ startActivity(emailIntent);
+ } else {
+ Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.menu_support_error1), Toast.LENGTH_LONG).show();
+ }
+
+ return false;
+ }
+ });
+
+ privacyPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ ExternalLinkActivity.launch(
+ getActivity(),
+ getString(R.string.preferences_privacy),
+ GlucosioExternalLinks.PRIVACY);
+ addTermsAnalyticsEvent("Glucosio Privacy opened");
+ return false;
+ }
+ });
+
+ thanksPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ ExternalLinkActivity.launch(
+ getActivity(),
+ getString(R.string.preferences_contributors),
+ GlucosioExternalLinks.THANKS);
+ addTermsAnalyticsEvent("Glucosio Contributors opened");
+ return false;
+ }
+ });
+
+ versionPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+
+ int easterEggCount;
+
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ if (easterEggCount == 6) {
+ String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 40.794010, 17.124583);
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
+ getActivity().startActivity(intent);
+ easterEggCount = 0;
+ } else {
+ this.easterEggCount = easterEggCount + 1;
+ }
+ return false;
+ }
+ });
+
+ }
+
+ private void addTermsAnalyticsEvent(String action) {
+ Analytics analytics = ((GlucosioApplication) getActivity().getApplication()).getAnalytics();
+
+ analytics.reportAction("Preferences", action);
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddA1CActivity.java b/app/src/main/java/org/glucosio/android/activity/AddA1CActivity.java
new file mode 100644
index 0000000..9e56e02
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddA1CActivity.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.os.Bundle;
+import android.support.v7.widget.Toolbar;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.HB1ACReading;
+import org.glucosio.android.presenter.AddA1CPresenter;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.Calendar;
+
+public class AddA1CActivity extends AddReadingActivity {
+
+ private TextView readingTextView;
+ private TextView unitTextView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_add_hb1ac);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddA1CPresenter presenter = new AddA1CPresenter(this);
+ setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ readingTextView = findViewById(R.id.hb1ac_add_value);
+ unitTextView = findViewById(R.id.hb1ac_unit);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+ if (!"percentage".equals(presenter.getA1CUnitMeasuerement())) {
+ unitTextView.setText(getString(R.string.mmol_mol));
+ }
+
+ // If an id is passed, open the activity in edit mode
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ setTitle(R.string.title_activity_add_hb1ac_edit);
+ HB1ACReading readingToEdit = presenter.getHB1ACReadingById(getEditId());
+ readingTextView.setText(numberFormat.format(readingToEdit.getReading()));
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(formatDateTime.getDate(cal));
+ this.getAddTimeTextView().setText(formatDateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ } else {
+ this.getAddDateTextView().setText(formatDateTime.getCurrentDate());
+ this.getAddTimeTextView().setText(formatDateTime.getCurrentTime());
+ }
+
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddA1CPresenter presenter = (AddA1CPresenter) getPresenter();
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString());
+ }
+ }
+
+ public void showErrorMessage() {
+ Toast.makeText(getApplicationContext(), getString(R.string.dialog_error2), Toast.LENGTH_SHORT).show();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddCholesterolActivity.java b/app/src/main/java/org/glucosio/android/activity/AddCholesterolActivity.java
new file mode 100644
index 0000000..f6bf70e
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddCholesterolActivity.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.os.Bundle;
+import android.support.v7.widget.Toolbar;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.CholesterolReading;
+import org.glucosio.android.presenter.AddCholesterolPresenter;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.Calendar;
+
+public class AddCholesterolActivity extends AddReadingActivity {
+
+ private TextView totalChoTextView;
+ private TextView LDLChoTextView;
+ private TextView HDLChoTextView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_add_cholesterol);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddCholesterolPresenter presenter = new AddCholesterolPresenter(this);
+ setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ totalChoTextView = findViewById(R.id.cholesterol_add_value_total);
+ LDLChoTextView = findViewById(R.id.cholesterol_add_value_ldl);
+ HDLChoTextView = findViewById(R.id.cholesterol_add_value_hdl);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+
+ // If an id is passed, open the activity in edit mode
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ FormatDateTime dateTime = new FormatDateTime(getApplicationContext());
+ setTitle(R.string.title_activity_add_cholesterol_edit);
+ CholesterolReading readingToEdit = presenter.getCholesterolReadingById(this.getEditId());
+
+ totalChoTextView.setText(numberFormat.format(readingToEdit.getTotalReading()));
+ LDLChoTextView.setText(numberFormat.format(readingToEdit.getLDLReading()));
+ HDLChoTextView.setText(numberFormat.format(readingToEdit.getHDLReading()));
+
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(dateTime.getDate(cal));
+ this.getAddTimeTextView().setText(dateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ } else {
+ this.getAddDateTextView().setText(formatDateTime.getCurrentDate());
+ this.getAddTimeTextView().setText(formatDateTime.getCurrentTime());
+ }
+
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddCholesterolPresenter presenter = (AddCholesterolPresenter) getPresenter();
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), totalChoTextView.getText().toString(), LDLChoTextView.getText().toString(), HDLChoTextView.getText().toString(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), totalChoTextView.getText().toString(), LDLChoTextView.getText().toString(), HDLChoTextView.getText().toString());
+ }
+ }
+
+ public void showErrorMessage() {
+ Toast.makeText(getApplicationContext(), getString(R.string.dialog_error2), Toast.LENGTH_SHORT).show();
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/glucosio/android/activity/AddGlucoseActivity.java b/app/src/main/java/org/glucosio/android/activity/AddGlucoseActivity.java
new file mode 100644
index 0000000..5bcbaea
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddGlucoseActivity.java
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.design.widget.Snackbar;
+import android.support.design.widget.TextInputLayout;
+import android.support.v7.widget.AppCompatButton;
+import android.support.v7.widget.Toolbar;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.EditText;
+import android.widget.TextView;
+import android.widget.TimePicker;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.db.GlucoseReading;
+import org.glucosio.android.presenter.AddGlucosePresenter;
+import org.glucosio.android.tools.FormatDateTime;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.LabelledSpinner;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.text.DecimalFormat;
+import java.util.Arrays;
+import java.util.Calendar;
+
+
+public class AddGlucoseActivity extends AddReadingActivity {
+
+ private static final int CUSTOM_TYPE_SPINNER_VALUE = 11;
+
+ private TextView readingTextView;
+ private EditText typeCustomEditText;
+ private EditText notesEditText;
+ private LabelledSpinner readingTypeSpinner;
+ private boolean isCustomType = false;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_add_glucose);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddGlucosePresenter presenter = new AddGlucosePresenter(this);
+ setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ readingTypeSpinner = findViewById(R.id.glucose_add_reading_type);
+ readingTypeSpinner.setItemsArray(R.array.dialog_add_measured_list);
+ readingTextView = findViewById(R.id.glucose_add_concentration);
+ typeCustomEditText = findViewById(R.id.glucose_type_custom);
+ TextInputLayout readingInputLayout = findViewById(R.id.glucose_add_concentration_layout);
+ AppCompatButton addFreeStyleButton = findViewById(R.id.glucose_add_freestyle_button);
+ notesEditText = findViewById(R.id.glucose_add_notes);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+ readingTypeSpinner.setOnItemChosenListener(new LabelledSpinner.OnItemChosenListener() {
+ @Override
+ public void onItemChosen(View labelledSpinner, AdapterView> adapterView, View itemView, int position, long id) {
+ // If other is selected
+ if (position == CUSTOM_TYPE_SPINNER_VALUE) {
+ typeCustomEditText.setVisibility(View.VISIBLE);
+ isCustomType = true;
+ } else {
+ if (typeCustomEditText.getVisibility() == View.VISIBLE) {
+ typeCustomEditText.setVisibility(View.GONE);
+ isCustomType = false;
+ }
+ }
+ }
+
+ @Override
+ public void onNothingChosen(View labelledSpinner, AdapterView> adapterView) {
+
+ }
+ });
+
+ TextView unitM = findViewById(R.id.glucose_add_unit_measurement);
+
+ if (Constants.Units.MG_DL.equals(presenter.getUnitMeasurement())) {
+ unitM.setText(getString(R.string.mg_dL));
+ } else {
+ unitM.setText(getString(R.string.mmol_L));
+ }
+
+ // If an id is passed, open the activity in edit mode
+ Calendar cal = Calendar.getInstance();
+ FormatDateTime dateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ setTitle(R.string.title_activity_add_glucose_edit);
+ GlucoseReading readingToEdit = presenter.getGlucoseReadingById(this.getEditId());
+
+ String readingString;
+ if (presenter.getUnitMeasurement().equals(Constants.Units.MG_DL)) {
+ readingString = String.valueOf(numberFormat.format(readingToEdit.getReading()));
+ } else {
+ readingString = String.valueOf(numberFormat.format(GlucosioConverter.glucoseToMmolL(readingToEdit.getReading())));
+ }
+
+ readingTextView.setText(readingString);
+ notesEditText.setText(readingToEdit.getNotes());
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(dateTime.getDate(cal));
+ this.getAddTimeTextView().setText(dateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ // retrieve spinner reading to set the registered one
+ String measuredTypeText = readingToEdit.getReading_type();
+ Integer measuredId = presenter.retrieveSpinnerID(measuredTypeText, Arrays.asList(getResources().getStringArray(R.array.dialog_add_measured_list)));
+ if (measuredId == null) { // if nothing, it a custom type
+ this.isCustomType = true;
+ readingTypeSpinner.setSelection(CUSTOM_TYPE_SPINNER_VALUE);
+ } else {
+ readingTypeSpinner.setSelection(measuredId);
+ }
+ if (this.isCustomType) {
+ typeCustomEditText.setText(measuredTypeText);
+ }
+ } else {
+ this.getAddDateTextView().setText(dateTime.getDate(cal));
+ this.getAddTimeTextView().setText(dateTime.getTime(cal));
+ presenter.updateSpinnerTypeTime();
+ }
+
+
+ // Check if activity was started from a NFC sensor
+ if (getIntent().getExtras() != null) {
+ Bundle p;
+ String reading;
+
+ p = getIntent().getExtras();
+ reading = p.getString("reading");
+ if (reading != null) {
+ double readingDouble = ReadingTools.safeParseDouble(reading);
+ readingTextView.setText(numberFormat.format(readingDouble));
+ readingInputLayout.setErrorEnabled(true);
+ readingInputLayout.setError(getResources().getString(R.string.dialog_add_glucose_freestylelibre_added));
+ addFreeStyleButton.setVisibility(View.GONE);
+
+ addAnalyticsEvent();
+ }
+ }
+
+ // Check if FreeStyle support is enabled in Preferences
+ if (presenter.isFreeStyleLibreEnabled()) {
+ addFreeStyleButton.setVisibility(View.VISIBLE);
+ addFreeStyleButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ startLibreActivity();
+ }
+ });
+ }
+ }
+
+ private void addAnalyticsEvent() {
+ Analytics analytics = ((GlucosioApplication) getApplication()).getAnalytics();
+ analytics.reportAction("FreeStyle Libre", "New reading added");
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddGlucosePresenter presenter = (AddGlucosePresenter) getPresenter();
+ String readingType;
+ if (isCustomType) {
+ readingType = typeCustomEditText.getText().toString();
+ } else {
+ readingType = readingTypeSpinner.getSpinner().getSelectedItem().toString();
+ }
+
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString(),
+ readingType, notesEditText.getText().toString(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString(),
+ readingType, notesEditText.getText().toString());
+ }
+ }
+
+ public void showErrorMessage() {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.dialog_error2), Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void startLibreActivity() {
+ Intent intent = new Intent(this, FreestyleLibreActivity.class);
+ startActivity(intent);
+ }
+
+ public void showDuplicateErrorMessage() {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.dialog_error_duplicate), Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void updateSpinnerTypeTime(int selection) {
+ readingTypeSpinner.setSelection(selection);
+ }
+
+ private void updateSpinnerTypeHour(int hour) {
+ AddGlucosePresenter presenter = (AddGlucosePresenter) getPresenter();
+ readingTypeSpinner.setSelection(presenter.hourToSpinnerType(hour));
+ }
+
+ @Override
+ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
+ super.onTimeSet(view, hourOfDay, minute);
+ DecimalFormat df = new DecimalFormat("00");
+ updateSpinnerTypeHour(Integer.parseInt(df.format(hourOfDay)));
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddKetoneActivity.java b/app/src/main/java/org/glucosio/android/activity/AddKetoneActivity.java
new file mode 100644
index 0000000..1edde20
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddKetoneActivity.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.os.Bundle;
+import android.support.v7.widget.Toolbar;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.KetoneReading;
+import org.glucosio.android.presenter.AddKetonePresenter;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.Calendar;
+
+public class AddKetoneActivity extends AddReadingActivity {
+
+ private TextView readingTextView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_add_ketone);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddKetonePresenter presenter = new AddKetonePresenter(this);
+ this.setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ readingTextView = findViewById(R.id.ketone_add_value);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+ // If an id is passed, open the activity in edit mode
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ setTitle(R.string.title_activity_add_ketone_edit);
+ KetoneReading readingToEdit = presenter.getKetoneReadingById(this.getEditId());
+ readingTextView.setText(numberFormat.format(readingToEdit.getReading()));
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(formatDateTime.getDate(cal));
+ this.getAddTimeTextView().setText(formatDateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ } else {
+ this.getAddDateTextView().setText(formatDateTime.getCurrentDate());
+ this.getAddTimeTextView().setText(formatDateTime.getCurrentTime());
+ }
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddKetonePresenter presenter = (AddKetonePresenter) getPresenter();
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString().trim(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString().trim());
+ }
+ }
+
+ public void showErrorMessage() {
+ Toast.makeText(getApplicationContext(), getString(R.string.dialog_error2), Toast.LENGTH_SHORT).show();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddPressureActivity.java b/app/src/main/java/org/glucosio/android/activity/AddPressureActivity.java
new file mode 100644
index 0000000..39dd2a2
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddPressureActivity.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.os.Bundle;
+import android.support.v7.widget.Toolbar;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.PressureReading;
+import org.glucosio.android.presenter.AddPressurePresenter;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.Calendar;
+
+public class AddPressureActivity extends AddReadingActivity {
+
+ private TextView minPressureTextView;
+ private TextView maxPressureTextView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_add_pressure);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddPressurePresenter presenter = new AddPressurePresenter(this);
+ setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ minPressureTextView = findViewById(R.id.pressure_add_value_min);
+ maxPressureTextView = findViewById(R.id.pressure_add_value_max);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+ // Initialize value
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ // set edit title
+ setTitle(R.string.title_activity_add_pressure_edit);
+ PressureReading readingToEdit = presenter.getPressureReadingById(this.getEditId());
+
+ // set reading values
+ minPressureTextView.setText(numberFormat.format(readingToEdit.getMinReading()));
+ maxPressureTextView.setText(numberFormat.format(readingToEdit.getMaxReading()));
+
+ // set reading time
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(formatDateTime.getDate(cal));
+ this.getAddTimeTextView().setText(formatDateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ } else {
+ this.getAddDateTextView().setText(formatDateTime.getCurrentDate());
+ this.getAddTimeTextView().setText(formatDateTime.getCurrentTime());
+ }
+
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddPressurePresenter presenter = (AddPressurePresenter) getPresenter();
+ // If an id is passed, open the activity in edit mode
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), minPressureTextView.getText().toString(), maxPressureTextView.getText().toString(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), minPressureTextView.getText().toString(), maxPressureTextView.getText().toString());
+ }
+ }
+
+ public void showErrorMessage() {
+ Toast.makeText(getApplicationContext(), getString(R.string.dialog_error2), Toast.LENGTH_SHORT).show();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddReadingActivity.java b/app/src/main/java/org/glucosio/android/activity/AddReadingActivity.java
new file mode 100644
index 0000000..b595e72
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddReadingActivity.java
@@ -0,0 +1,237 @@
+package org.glucosio.android.activity;
+
+import android.app.DatePickerDialog;
+import android.app.TimePickerDialog;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.design.widget.FloatingActionButton;
+import android.support.v7.app.AppCompatActivity;
+import android.view.KeyEvent;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.DatePicker;
+import android.widget.TextView;
+import android.widget.TimePicker;
+
+import org.glucosio.android.R;
+import org.glucosio.android.presenter.AddReadingPresenter;
+import org.glucosio.android.tools.AnimationTools;
+import org.glucosio.android.tools.FormatDateTime;
+import org.glucosio.android.tools.NumberFormatUtils;
+
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Calendar;
+
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public abstract class AddReadingActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
+
+ protected final NumberFormat numberFormat = NumberFormatUtils.createDefaultNumberFormat();
+ private final java.lang.String INTENT_EXTRA_EDIT = "editing";
+ private final java.lang.String INTENT_EXTRA_EDIT_ID = "edit_id";
+ private final String INTENT_EXTRA_PAGER = "pager";
+ private final String INTENT_EXTRA_DROPDOWN = "history_dropdown";
+ private AddReadingPresenter presenter;
+
+ private TextView addTimeTextView;
+ private TextView addDateTextView;
+ private FloatingActionButton doneFAB;
+ private Runnable fabAnimationRunnable;
+
+ private int pagerPosition;
+ private int dropdownPosition;
+ private long editId = 0;
+ private boolean editing = false;
+
+ protected void retrieveExtra() {
+ Bundle b = getIntent().getExtras();
+ if (b != null) {
+ pagerPosition = b.getInt(INTENT_EXTRA_PAGER);
+ editId = b.getLong(INTENT_EXTRA_EDIT_ID);
+ editing = b.getBoolean(INTENT_EXTRA_EDIT);
+ dropdownPosition = b.getInt(INTENT_EXTRA_DROPDOWN);
+ }
+ }
+
+ @Override
+ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
+ TextView addTime = findViewById(R.id.dialog_add_time);
+ DecimalFormat df = new DecimalFormat("00");
+
+ presenter.setReadingHour(df.format(hourOfDay));
+ presenter.setReadingMinute(df.format(minute));
+
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
+ cal.set(Calendar.MINUTE, minute);
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ addTime.setText(formatDateTime.getTime(cal));
+ }
+
+ @Override
+ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
+ TextView addDate = findViewById(R.id.dialog_add_date);
+ DecimalFormat df = new DecimalFormat("00");
+
+ presenter.setReadingYear(year + "");
+ presenter.setReadingMonth(df.format(monthOfYear + 1));
+ presenter.setReadingDay(df.format(dayOfMonth));
+
+ String date = +dayOfMonth + "/" + presenter.getReadingMonth() + "/" + presenter.getReadingYear();
+ addDate.setText(date);
+ }
+
+ public void createDateTimeViewAndListener() {
+ addTimeTextView = findViewById(R.id.dialog_add_time);
+ addDateTextView = findViewById(R.id.dialog_add_date);
+
+ addDateTextView.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Calendar now = Calendar.getInstance();
+ DatePickerDialog dpd = new DatePickerDialog(
+ AddReadingActivity.this,
+ AddReadingActivity.this,
+ now.get(Calendar.YEAR),
+ now.get(Calendar.MONTH),
+ now.get(Calendar.DAY_OF_MONTH)
+ );
+ dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
+ dpd.show();
+ }
+ });
+
+ addTimeTextView.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(getApplicationContext());
+ AddReadingActivity addReadingActivity = AddReadingActivity.this;
+ AddReadingPresenter addReadingPresenter = addReadingActivity.getPresenter();
+ Calendar cal = addReadingPresenter.getReadingCal();
+ if (addReadingActivity.isEditing()) {
+ cal.setTime(addReadingPresenter.getReadingTime());
+ }
+ TimePickerDialog tpd = new TimePickerDialog(
+ AddReadingActivity.this,
+ AddReadingActivity.this,
+ cal.get(Calendar.HOUR_OF_DAY),
+ cal.get(Calendar.MINUTE),
+ is24HourFormat);
+ tpd.show();
+ }
+ });
+ }
+
+ public void createFANViewAndListener() {
+
+ doneFAB = findViewById(R.id.done_fab);
+ doneFAB.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ dialogOnAddButtonPressed();
+ }
+ });
+ fabAnimationRunnable = new Runnable() {
+ @Override
+ public void run() {
+ AnimationTools.startCircularReveal(doneFAB);
+ }
+ };
+ doneFAB.postDelayed(fabAnimationRunnable, 600);
+ }
+
+ protected abstract void dialogOnAddButtonPressed();
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
+ && keyCode == KeyEvent.KEYCODE_BACK
+ && event.getRepeatCount() == 0) {
+ onBackPressed();
+ return true;
+ }
+ return super.onKeyDown(keyCode, event);
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case android.R.id.home:
+ onBackPressed();
+ return true;
+ default:
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
+ @Override
+ public void onBackPressed() {
+ finishActivity();
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+
+ public void finishActivity() {
+ Intent intent = new Intent(this, MainActivity.class);
+ // Pass pager position to open it again later
+ Bundle b = new Bundle();
+ b.putInt(INTENT_EXTRA_PAGER, this.getPagerPosition());
+ b.putInt(INTENT_EXTRA_DROPDOWN, this.getDropdownPosition());
+ intent.putExtras(b);
+ startActivity(intent);
+ finish();
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ doneFAB.removeCallbacks(fabAnimationRunnable);
+ }
+
+ public AddReadingPresenter getPresenter() {
+ return this.presenter;
+ }
+
+ // Getter and Setter
+ public void setPresenter(AddReadingPresenter newPresenter) {
+ this.presenter = newPresenter;
+ }
+
+ public int getPagerPosition() {
+ return pagerPosition;
+ }
+
+ public int getDropdownPosition() {
+ return dropdownPosition;
+ }
+
+ public long getEditId() {
+ return editId;
+ }
+
+ public boolean isEditing() {
+ return editing;
+ }
+
+ public TextView getAddTimeTextView() {
+ return addTimeTextView;
+ }
+
+ public void setAddTimeTextView(TextView addTimeTextView) {
+ this.addTimeTextView = addTimeTextView;
+ }
+
+ public TextView getAddDateTextView() {
+ return addDateTextView;
+ }
+
+ public void setAddDateTextView(TextView addDateTextView) {
+ this.addDateTextView = addDateTextView;
+ }
+
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/AddWeightActivity.java b/app/src/main/java/org/glucosio/android/activity/AddWeightActivity.java
new file mode 100644
index 0000000..92cc06b
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/AddWeightActivity.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.os.Bundle;
+import android.support.v7.widget.Toolbar;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.WeightReading;
+import org.glucosio.android.presenter.AddWeightPresenter;
+import org.glucosio.android.tools.FormatDateTime;
+import org.glucosio.android.tools.GlucosioConverter;
+
+import java.util.Calendar;
+
+public class AddWeightActivity extends AddReadingActivity {
+
+ private TextView readingTextView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ boolean needUnitConversion = false;
+
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.activity_add_weight);
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ }
+
+ this.retrieveExtra();
+
+ AddWeightPresenter presenter = new AddWeightPresenter(this);
+ this.setPresenter(presenter);
+ presenter.setReadingTimeNow();
+
+ readingTextView = findViewById(R.id.weight_add_value);
+ TextView unitTextView = findViewById(R.id.weight_add_unit_measurement);
+
+ this.createDateTimeViewAndListener();
+ this.createFANViewAndListener();
+
+ if (!"kilograms".equals(presenter.getWeightUnitMeasuerement())) {
+ unitTextView.setText("lbs");
+ needUnitConversion = true;
+ }
+
+ // If an id is passed, open the activity in edit mode
+ FormatDateTime formatDateTime = new FormatDateTime(getApplicationContext());
+ if (this.isEditing()) {
+ setTitle(R.string.title_activity_add_weight_edit);
+ WeightReading readingToEdit = presenter.getWeightReadingById(this.getEditId());
+ double weightVal = readingToEdit.getReading();
+ if (needUnitConversion)
+ weightVal = GlucosioConverter.kgToLb(weightVal);
+ readingTextView.setText(numberFormat.format(weightVal));
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(readingToEdit.getCreated());
+ this.getAddDateTextView().setText(formatDateTime.getDate(cal));
+ this.getAddTimeTextView().setText(formatDateTime.getTime(cal));
+ presenter.updateReadingSplitDateTime(readingToEdit.getCreated());
+ } else {
+ this.getAddDateTextView().setText(formatDateTime.getCurrentDate());
+ this.getAddTimeTextView().setText(formatDateTime.getCurrentTime());
+ }
+
+ }
+
+ @Override
+ protected void dialogOnAddButtonPressed() {
+ AddWeightPresenter presenter = (AddWeightPresenter) this.getPresenter();
+ if (this.isEditing()) {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString(), this.getEditId());
+ } else {
+ presenter.dialogOnAddButtonPressed(this.getAddTimeTextView().getText().toString(),
+ this.getAddDateTextView().getText().toString(), readingTextView.getText().toString());
+
+ }
+ }
+
+ public void showErrorMessage() {
+ Toast.makeText(getApplicationContext(), getString(R.string.dialog_error2), Toast.LENGTH_SHORT).show();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/BackupActivity.java b/app/src/main/java/org/glucosio/android/activity/BackupActivity.java
new file mode 100644
index 0000000..7cd1eab
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/BackupActivity.java
@@ -0,0 +1,514 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.app.Activity;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentSender;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.app.AppCompatActivity;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.Button;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView;
+import com.google.android.gms.common.api.GoogleApiClient;
+import com.google.android.gms.common.api.ResultCallback;
+import com.google.android.gms.drive.Drive;
+import com.google.android.gms.drive.DriveApi;
+import com.google.android.gms.drive.DriveContents;
+import com.google.android.gms.drive.DriveFile;
+import com.google.android.gms.drive.DriveFolder;
+import com.google.android.gms.drive.DriveId;
+import com.google.android.gms.drive.DriveResource;
+import com.google.android.gms.drive.Metadata;
+import com.google.android.gms.drive.MetadataBuffer;
+import com.google.android.gms.drive.MetadataChangeSet;
+import com.google.android.gms.drive.OpenFileActivityBuilder;
+import com.google.android.gms.drive.query.Filters;
+import com.google.android.gms.drive.query.Query;
+import com.google.android.gms.drive.query.SearchableField;
+import com.google.android.gms.drive.query.SortOrder;
+import com.google.android.gms.drive.query.SortableField;
+import com.google.firebase.crash.FirebaseCrash;
+
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.adapter.BackupAdapter;
+import org.glucosio.android.backup.Backup;
+import org.glucosio.android.object.GlucosioBackup;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Date;
+
+import io.realm.Realm;
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public class BackupActivity extends AppCompatActivity {
+ private static final int REQUEST_CODE_PICKER = 2;
+ private static final int REQUEST_CODE_PICKER_FOLDER = 4;
+
+ private static final String TAG = "glucosio_drive_backup";
+ private static final String BACKUP_FOLDER_KEY = "backup_folder";
+
+ private Backup backup;
+ private GoogleApiClient mGoogleApiClient;
+ private TextView folderTextView;
+ private IntentSender intentPicker;
+ private Realm realm;
+ private String backupFolder;
+ private ExpandableHeightListView backupListView;
+
+ private SharedPreferences sharedPref;
+
+ @Override
+ public void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.backup_drive_activity);
+
+ GlucosioApplication glucosioApplication = (GlucosioApplication) getApplicationContext();
+ sharedPref = getPreferences(Context.MODE_PRIVATE);
+ realm = glucosioApplication.getDBHandler().getRealmInstance();
+
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setTitle(getResources().getString(R.string.title_activity_backup_drive));
+
+ backup = glucosioApplication.getBackup();
+ backup.init(this);
+ connectClient();
+ mGoogleApiClient = backup.getClient();
+
+ Button backupButton = findViewById(R.id.activity_backup_drive_button_backup);
+ TextView manageButton = findViewById(R.id.activity_backup_drive_button_manage_drive);
+ folderTextView = findViewById(R.id.activity_backup_drive_textview_folder);
+ LinearLayout selectFolderButton = findViewById(R.id.activity_backup_drive_button_folder);
+ backupListView = findViewById(R.id.activity_backup_drive_listview_restore);
+
+ backupListView.setExpanded(true);
+
+ backupButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ // Open Folder picker, then upload the file on Drive
+ openFolderPicker(true);
+ }
+ });
+
+ selectFolderButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ // Check first if a folder is already selected
+ if (!"".equals(backupFolder)) {
+ //Start the picker to choose a folder
+ //False because we don't want to upload the backup on drive then
+ openFolderPicker(false);
+ }
+ }
+ });
+
+ manageButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ openOnDrive(DriveId.decodeFromString(backupFolder));
+ }
+ });
+
+ // Show backup folder, if exists
+ backupFolder = sharedPref.getString(BACKUP_FOLDER_KEY, "");
+ if (!("").equals(backupFolder)) {
+ setBackupFolderTitle(DriveId.decodeFromString(backupFolder));
+ manageButton.setVisibility(View.VISIBLE);
+ }
+
+ // Populate backup list
+ if (!("").equals(backupFolder)) {
+ getBackupsFromDrive(DriveId.decodeFromString(backupFolder).asDriveFolder());
+ }
+ }
+
+ private void setBackupFolderTitle(DriveId id) {
+ id.asDriveFolder().getMetadata((mGoogleApiClient)).setResultCallback(
+ new ResultCallback() {
+ @Override
+ public void onResult(@NonNull DriveResource.MetadataResult result) {
+ if (!result.getStatus().isSuccess()) {
+ showErrorDialog();
+ return;
+ }
+ Metadata metadata = result.getMetadata();
+ folderTextView.setText(metadata.getTitle());
+ }
+ }
+ );
+ }
+
+ private void openFolderPicker(boolean uploadToDrive) {
+ if (uploadToDrive) {
+ // First we check if a backup folder is set
+ if (TextUtils.isEmpty(backupFolder)) {
+ try {
+ if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
+ if (intentPicker == null)
+ intentPicker = buildIntent();
+ //Start the picker to choose a folder
+ startIntentSenderForResult(
+ intentPicker, REQUEST_CODE_PICKER, null, 0, 0, 0);
+ }
+ } catch (IntentSender.SendIntentException e) {
+ Log.e(TAG, "Unable to send intent", e);
+ showErrorDialog();
+ }
+ } else {
+ uploadToDrive(DriveId.decodeFromString(backupFolder));
+ }
+ } else {
+ try {
+ intentPicker = null;
+ if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
+ if (intentPicker == null)
+ intentPicker = buildIntent();
+ //Start the picker to choose a folder
+ startIntentSenderForResult(
+ intentPicker, REQUEST_CODE_PICKER_FOLDER, null, 0, 0, 0);
+ }
+ } catch (IntentSender.SendIntentException e) {
+ Log.e(TAG, "Unable to send intent", e);
+ showErrorDialog();
+ }
+ }
+ }
+
+ private IntentSender buildIntent() {
+ return Drive.DriveApi
+ .newOpenFileActivityBuilder()
+ .setMimeType(new String[]{DriveFolder.MIME_TYPE})
+ .build(mGoogleApiClient);
+ }
+
+ private void getBackupsFromDrive(DriveFolder folder) {
+ final Activity activity = this;
+ SortOrder sortOrder = new SortOrder.Builder()
+ .addSortDescending(SortableField.MODIFIED_DATE).build();
+ Query query = new Query.Builder()
+ .addFilter(Filters.eq(SearchableField.TITLE, "glucosio.realm"))
+ .addFilter(Filters.eq(SearchableField.TRASHED, false))
+ .setSortOrder(sortOrder)
+ .build();
+ folder.queryChildren(mGoogleApiClient, query)
+ .setResultCallback(new ResultCallback() {
+
+ private ArrayList backupsArray = new ArrayList<>();
+
+ @Override
+ public void onResult(@NonNull DriveApi.MetadataBufferResult result) {
+ MetadataBuffer buffer = result.getMetadataBuffer();
+ int size = buffer.getCount();
+ for (int i = 0; i < size; i++) {
+ Metadata metadata = buffer.get(i);
+ DriveId driveId = metadata.getDriveId();
+ Date modifiedDate = metadata.getModifiedDate();
+ long backupSize = metadata.getFileSize();
+ backupsArray.add(new GlucosioBackup(driveId, modifiedDate, backupSize));
+ }
+ backupListView.setAdapter(new BackupAdapter(activity, R.layout.activity_backup_drive_restore_item, backupsArray));
+ }
+ });
+ }
+
+ public void downloadFromDrive(DriveFile file) {
+ file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
+ .setResultCallback(new ResultCallback() {
+ @Override
+ public void onResult(@NonNull DriveApi.DriveContentsResult result) {
+ if (!result.getStatus().isSuccess()) {
+ showErrorDialog();
+ return;
+ }
+
+ // DriveContents object contains pointers
+ // to the actual byte stream
+ DriveContents contents = result.getDriveContents();
+ InputStream input = contents.getInputStream();
+
+ try {
+ File file = new File(realm.getPath());
+ OutputStream output = new FileOutputStream(file);
+ try {
+ try {
+ byte[] buffer = new byte[4 * 1024]; // or other buffer size
+ int read;
+
+ while ((read = input.read(buffer)) != -1) {
+ output.write(buffer, 0, read);
+ }
+ output.flush();
+ } finally {
+ safeCloseClosable(input);
+ }
+ } catch (Exception e) {
+ reportToFirebase(e, "Error downloading backup from drive");
+ e.printStackTrace();
+ }
+ } catch (FileNotFoundException e) {
+ reportToFirebase(e, "Error downloading backup from drive, file not found");
+ e.printStackTrace();
+ } finally {
+ safeCloseClosable(input);
+ }
+
+ Toast.makeText(getApplicationContext(), R.string.activity_backup_drive_message_restart, Toast.LENGTH_LONG).show();
+
+ // Reboot app
+ Intent mStartActivity = new Intent(getApplicationContext(), MainActivity.class);
+ int mPendingIntentId = 123456;
+ PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+ AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ mgr.setExact(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+ } else {
+ mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+ }
+ System.exit(0);
+ }
+ });
+ }
+
+ private void safeCloseClosable(Closeable closeable) {
+ try {
+ closeable.close();
+ } catch (IOException e) {
+ reportToFirebase(e, "Error downloading backup from drive, IO Exception");
+ e.printStackTrace();
+ }
+ }
+
+ private void uploadToDrive(DriveId mFolderDriveId) {
+ if (mFolderDriveId != null) {
+ //Create the file on GDrive
+ final DriveFolder folder = mFolderDriveId.asDriveFolder();
+ Drive.DriveApi.newDriveContents(mGoogleApiClient)
+ .setResultCallback(new ResultCallback() {
+ @Override
+ public void onResult(@NonNull DriveApi.DriveContentsResult result) {
+ if (!result.getStatus().isSuccess()) {
+ Log.e(TAG, "Error while trying to create new file contents");
+ showErrorDialog();
+ return;
+ }
+ final DriveContents driveContents = result.getDriveContents();
+
+ // Perform I/O off the UI thread.
+ new Thread() {
+ @Override
+ public void run() {
+ // write content to DriveContents
+ OutputStream outputStream = driveContents.getOutputStream();
+
+ FileInputStream inputStream = null;
+ try {
+ inputStream = new FileInputStream(new File(realm.getPath()));
+ } catch (FileNotFoundException e) {
+ reportToFirebase(e, "Error uploading backup from drive, file not found");
+ showErrorDialog();
+ e.printStackTrace();
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException ignored) {
+ //ignored
+ }
+ }
+ }
+
+ byte[] buf = new byte[1024];
+ int bytesRead;
+ try {
+ if (inputStream != null) {
+ while ((bytesRead = inputStream.read(buf)) > 0) {
+ outputStream.write(buf, 0, bytesRead);
+ }
+ }
+ } catch (IOException e) {
+
+ showErrorDialog();
+ e.printStackTrace();
+ }
+
+
+ MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
+ .setTitle("glucosio.realm")
+ .setMimeType("text/plain")
+ .build();
+
+ // create a file in selected folder
+ folder.createFile(mGoogleApiClient, changeSet, driveContents)
+ .setResultCallback(new ResultCallback() {
+ @Override
+ public void onResult(@NonNull DriveFolder.DriveFileResult result) {
+ if (!result.getStatus().isSuccess()) {
+ Log.d(TAG, "Error while trying to create the file");
+ showErrorDialog();
+ finish();
+ return;
+ }
+ showSuccessDialog();
+ finish();
+ }
+ });
+ }
+ }.start();
+ }
+ });
+ }
+ }
+
+ private void openOnDrive(DriveId driveId) {
+ driveId.asDriveFolder().getMetadata((mGoogleApiClient)).setResultCallback(
+ new ResultCallback() {
+ @Override
+ public void onResult(@NonNull DriveResource.MetadataResult result) {
+ if (!result.getStatus().isSuccess()) {
+ showErrorDialog();
+ return;
+ }
+ Metadata metadata = result.getMetadata();
+ String url = metadata.getAlternateLink();
+ Intent i = new Intent(Intent.ACTION_VIEW);
+ i.setData(Uri.parse(url));
+ startActivity(i);
+ }
+ }
+ );
+ }
+
+ @Override
+ protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
+ switch (requestCode) {
+ case 1:
+ if (resultCode == RESULT_OK) {
+ backup.start();
+ }
+ break;
+ // REQUEST_CODE_PICKER
+ case 2:
+ intentPicker = null;
+
+ if (resultCode == RESULT_OK) {
+ //Get the folder drive id
+ DriveId mFolderDriveId = data.getParcelableExtra(
+ OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
+
+ saveBackupFolder(mFolderDriveId.encodeToString());
+
+ uploadToDrive(mFolderDriveId);
+ }
+ break;
+
+ // REQUEST_CODE_SELECT
+ case 3:
+ if (resultCode == RESULT_OK) {
+ // get the selected item's ID
+ DriveId driveId = data.getParcelableExtra(
+ OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
+
+ DriveFile file = driveId.asDriveFile();
+ downloadFromDrive(file);
+
+ } else {
+ showErrorDialog();
+ }
+ finish();
+ break;
+ // REQUEST_CODE_PICKER_FOLDER
+ case 4:
+ if (resultCode == RESULT_OK) {
+ //Get the folder drive id
+ DriveId mFolderDriveId = data.getParcelableExtra(
+ OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
+
+ saveBackupFolder(mFolderDriveId.encodeToString());
+ // Restart activity to apply changes
+ Intent intent = getIntent();
+ finish();
+ startActivity(intent);
+ }
+ break;
+ }
+ }
+
+ private void saveBackupFolder(String folderPath) {
+ SharedPreferences.Editor editor = sharedPref.edit();
+ editor.putString(BACKUP_FOLDER_KEY, folderPath);
+ editor.apply();
+ }
+
+ private void showSuccessDialog() {
+ Toast.makeText(getApplicationContext(), R.string.activity_backup_drive_success, Toast.LENGTH_SHORT).show();
+ }
+
+ private void showErrorDialog() {
+ Toast.makeText(getApplicationContext(), R.string.activity_backup_drive_failed, Toast.LENGTH_SHORT).show();
+ }
+
+ private void reportToFirebase(Exception e, String message) {
+ FirebaseCrash.log(message);
+ FirebaseCrash.report(e);
+ }
+
+ public void connectClient() {
+ backup.start();
+ }
+
+ public void disconnectClient() {
+ backup.stop();
+ }
+
+ public boolean onOptionsItemSelected(MenuItem item) {
+ finish();
+ return true;
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/ExternalLinkActivity.java b/app/src/main/java/org/glucosio/android/activity/ExternalLinkActivity.java
new file mode 100644
index 0000000..70e5f09
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/ExternalLinkActivity.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.view.MenuItem;
+import android.webkit.WebResourceRequest;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import org.glucosio.android.R;
+import org.glucosio.android.presenter.ExternalViewPresenter;
+import org.glucosio.android.tools.network.BasicNetworkConnectivity;
+import org.glucosio.android.tools.network.GlucosioExternalLinks;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ExternalLinkActivity extends AppCompatActivity implements ExternalViewPresenter.View {
+
+ private static final String TITLE_KEY = "TITLE_KEY";
+ private static final String URL_KEY = "URL_KEY";
+ private ExternalViewPresenter presenter;
+ private WebView webView;
+ private Map toolbarTitle;
+
+ public static void launch(@NonNull Context context, @NonNull String title, String url) {
+ Intent intent = new Intent(context, ExternalLinkActivity.class);
+ intent.putExtra(TITLE_KEY, title);
+ intent.putExtra(URL_KEY, url);
+ context.startActivity(intent);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_licence);
+ webView = findViewById(R.id.webview_licence);
+ init();
+ }
+
+ private void init() {
+ initTitles();
+ initPresenter();
+ initView();
+ }
+
+ private void initView() {
+ webView.setWebViewClient(new WebViewClient() {
+ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
+ return true;
+ }
+ });
+ setupToolbar();
+ }
+
+ private void initPresenter() {
+ presenter = new ExternalViewPresenter(this, new BasicNetworkConnectivity(this));
+ presenter.onViewCreated();
+ }
+
+ @Override
+ public void setupToolbarTitle(String link) {
+ if (toolbarTitle.containsKey(link)) {
+ setToolbarTitle(getString(toolbarTitle.get(link)));
+ }
+ }
+
+ @Override
+ public String extractTitle() {
+ return extractExtra(TITLE_KEY);
+ }
+
+ @Override
+ public String extractUrl() {
+ return extractExtra(URL_KEY);
+ }
+
+ private String extractExtra(String key) {
+ String extra = "";
+ if (getIntent().getExtras() != null) {
+ extra = getIntent().getStringExtra(key);
+ }
+ return extra;
+ }
+
+ @Override
+ public void loadExternalUrl(String url) {
+ webView.loadUrl(url);
+ }
+
+ @Override
+ public void showNoConnectionWarning() {
+ AlertDialog dialog =
+ new AlertDialog.Builder(this).setMessage(R.string.warning_internet_connection_required)
+ .setCancelable(false)
+ .setPositiveButton(R.string.reading_ok, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ ExternalLinkActivity.this.finish();
+ }
+ })
+ .create();
+ dialog.show();
+ }
+
+ private void setupToolbar() {
+ if (getSupportActionBar() != null) {
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setDisplayShowHomeEnabled(true);
+ }
+ }
+
+ private void initTitles() {
+ toolbarTitle = new HashMap<>();
+ toolbarTitle.put(GlucosioExternalLinks.PRIVACY, R.string.preferences_privacy);
+ toolbarTitle.put(GlucosioExternalLinks.LICENSES, R.string.preferences_licences_open);
+ toolbarTitle.put(GlucosioExternalLinks.TERMS, R.string.preferences_terms);
+ }
+
+ private void setToolbarTitle(String string) {
+ if (getSupportActionBar() != null) {
+ getSupportActionBar().setTitle(string);
+ }
+ }
+
+ public boolean onOptionsItemSelected(MenuItem item) {
+ finish();
+ return true;
+ }
+}
+
+
diff --git a/app/src/main/java/org/glucosio/android/activity/FreestyleLibreActivity.java b/app/src/main/java/org/glucosio/android/activity/FreestyleLibreActivity.java
new file mode 100644
index 0000000..c278499
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/FreestyleLibreActivity.java
@@ -0,0 +1,265 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.app.Activity;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.nfc.NfcAdapter;
+import android.nfc.Tag;
+import android.nfc.tech.NfcV;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.User;
+import org.glucosio.android.object.PredictionData;
+import org.glucosio.android.object.ReadingData;
+import org.glucosio.android.tools.AlgorithmUtil;
+import org.glucosio.android.tools.AnimationTools;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+public class FreestyleLibreActivity extends Activity {
+
+ private static final String TAG = "FreestyleLibreActivity";
+
+ private NfcAdapter mNfcAdapter;
+ private TextView readingTextView;
+ private User user;
+ private ReadingData mResult = new ReadingData(PredictionData.Result.ERROR_NO_NFC);
+
+ /**
+ * @param activity The corresponding {@link Activity} requesting the foreground dispatch.
+ * @param adapter The {@link NfcAdapter} used for the foreground dispatch.
+ */
+ public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
+ final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
+ intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
+
+ final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
+
+ IntentFilter[] filters = new IntentFilter[1];
+ String[][] techList = new String[][]{};
+
+ // Notice that this is the same filter as in our manifest.
+ filters[0] = new IntentFilter();
+ filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
+ filters[0].addCategory(Intent.CATEGORY_DEFAULT);
+
+ adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
+ }
+
+ /**
+ * @param activity The corresponding {@link Activity} requesting to stop the foreground dispatch.
+ * @param adapter The {@link NfcAdapter} used for the foreground dispatch.
+ */
+ public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
+ adapter.disableForegroundDispatch(activity);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_freestyle_libre);
+
+ final GlucosioApplication app = (GlucosioApplication) getApplicationContext();
+ DatabaseHandler dB = app.getDBHandler();
+ user = dB.getUser(1);
+
+ mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
+ readingTextView = findViewById(R.id.activity_freestyle_textview_reading);
+ Button saveButton = findViewById(R.id.activity_freestyle_button_save);
+
+ saveButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ openAddGlucoseActivity();
+ }
+ });
+
+ if (mNfcAdapter == null) {
+ // Stop here, we definitely need NFC
+ Toast.makeText(this, R.string.freestylelibre_nfc_not_supported, Toast.LENGTH_LONG).show();
+ finish();
+ return;
+
+ }
+
+ if (!mNfcAdapter.isEnabled()) {
+ Toast.makeText(this, R.string.freestylelibre_nfc_not_enabled, Toast.LENGTH_LONG).show();
+ }
+
+ handleIntent(getIntent());
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ /**
+ * It's important, that the activity is in the foreground (resumed). Otherwise
+ * an IllegalStateException is thrown.
+ */
+ setupForegroundDispatch(this, mNfcAdapter);
+ }
+
+ @Override
+ protected void onPause() {
+ /**
+ * Call this before onPause, otherwise an IllegalArgumentException is thrown as well.
+ */
+ stopForegroundDispatch(this, mNfcAdapter);
+
+ super.onPause();
+ }
+
+ @Override
+ protected void onNewIntent(Intent intent) {
+ /**
+ * This method gets called, when a new Intent gets associated with the current activity instance.
+ * Instead of creating a new activity, onNewIntent will be called. For more information have a look
+ * at the documentation.
+ *
+ * In our case this method gets called, when the user attaches a Tag to the device.
+ */
+ handleIntent(intent);
+ }
+
+ private void handleIntent(Intent intent) {
+ String action = intent.getAction();
+ if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
+
+ Log.d("glucosio", "NfcAdapter.ACTION_TECH_DISCOVERED");
+ // In case we would still use the Tech Discovered Intent
+ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+ new NfcVReaderTask().execute(tag);
+ }
+ }
+
+ private void showReadingLayout() {
+ // Apply values in TextViews
+ readingTextView.setText(mResult.trend.get(0).glucose(Constants.Units.MMOL_L.equals(user.getPreferred_unit())));
+
+ View view = findViewById(R.id.activity_freestyle_reading);
+ view.setVisibility(View.INVISIBLE);
+ AnimationTools.startCircularReveal(view);
+ }
+
+ private void openAddGlucoseActivity() {
+ DatabaseHandler dB = new DatabaseHandler(getApplicationContext());
+ if (dB.getUser(1) != null) {
+ // Start AddGlucose Activity passing the reading value
+ Intent intent = new Intent(getApplicationContext(), AddGlucoseActivity.class);
+ Bundle bundle = new Bundle();
+ String currentGlucose = mResult.trend.get(0).glucose(user.getPreferred_unit().equals(Constants.Units.MMOL_L));
+ bundle.putString("reading", currentGlucose + "");
+ intent.putExtras(bundle);
+ startActivity(intent);
+ FreestyleLibreActivity.this.finish();
+ } else {
+ Intent intent = new Intent(getApplicationContext(), HelloActivity.class);
+ startActivity(intent);
+ }
+ }
+
+ private String bytesToHexString(byte[] src) {
+ StringBuilder builder = new StringBuilder();
+ if (src == null || src.length <= 0) {
+ return "";
+ }
+
+ char[] buffer = new char[2];
+ for (byte b : src) {
+ buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
+ buffer[1] = Character.forDigit(b & 0x0F, 16);
+ builder.append(buffer);
+ }
+
+ return builder.toString();
+ }
+
+ private class NfcVReaderTask extends AsyncTask {
+
+ private byte[] data = new byte[360];
+
+ @Override
+ protected void onPostExecute(Tag tag) {
+ if (tag == null) return;
+ String tagId = bytesToHexString(tag.getId());
+ int attempt = 1;
+ mResult = AlgorithmUtil.parseData(attempt, tagId, data);
+ showReadingLayout();
+ }
+
+ @Override
+ protected Tag doInBackground(Tag... params) {
+ Tag tag = params[0];
+ NfcV nfcvTag = NfcV.get(tag);
+ try {
+ nfcvTag.connect();
+ final byte[] uid = tag.getId();
+ for (int i = 0; i <= 40; i++) {
+ byte[] cmd = new byte[]{0x60, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, (byte) i, 0};
+ System.arraycopy(uid, 0, cmd, 2, 8);
+ byte[] oneBlock;
+ Long time = System.currentTimeMillis();
+ while (true) {
+ try {
+ oneBlock = nfcvTag.transceive(cmd);
+ break;
+ } catch (IOException e) {
+ if ((System.currentTimeMillis() > time + 2000)) {
+ Log.e(TAG, "tag read timeout");
+ return null;
+ }
+ }
+ }
+
+ oneBlock = Arrays.copyOfRange(oneBlock, 2, oneBlock.length);
+ System.arraycopy(oneBlock, 0, data, i * 8, 8);
+ }
+
+ } catch (Exception e) {
+ Log.i(TAG, e.toString());
+ return null;
+ } finally {
+ try {
+ nfcvTag.close();
+ } catch (Exception e) {
+ Log.e(TAG, "Error closing tag!");
+ }
+ }
+
+ return tag;
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/HelloActivity.java b/app/src/main/java/org/glucosio/android/activity/HelloActivity.java
new file mode 100644
index 0000000..0d53444
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/HelloActivity.java
@@ -0,0 +1,234 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.support.v4.content.res.ResourcesCompat;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.util.Log;
+import android.view.WindowManager;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.presenter.HelloPresenter;
+import org.glucosio.android.tools.LabelledSpinner;
+import org.glucosio.android.tools.LocaleHelper;
+import org.glucosio.android.tools.network.GlucosioExternalLinks;
+import org.glucosio.android.view.HelloView;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import butterknife.OnClick;
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public class HelloActivity extends AppCompatActivity implements HelloView {
+
+ @BindView(R.id.activity_hello_spinner_country)
+ LabelledSpinner countrySpinner;
+
+ @BindView(R.id.activity_hello_spinner_language)
+ LabelledSpinner languageSpinner;
+
+ @BindView(R.id.activity_hello_spinner_gender)
+ LabelledSpinner genderSpinner;
+
+ @BindView(R.id.activity_hello_spinner_diabetes_type)
+ LabelledSpinner typeSpinner;
+
+ @BindView(R.id.activity_hello_spinner_preferred_unit)
+ LabelledSpinner unitSpinner;
+
+ @BindView(R.id.activity_hello_button_start)
+ Button startButton;
+
+ @BindView(R.id.activity_hello_age)
+ TextView ageTextView;
+
+ private HelloPresenter presenter;
+
+ private List localesWithTranslation;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_hello);
+
+ ButterKnife.bind(this);
+
+ // Prevent SoftKeyboard to pop up on start
+ getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
+
+ GlucosioApplication application = (GlucosioApplication) getApplication();
+ presenter = application.createHelloPresenter(this);
+ presenter.loadDatabase();
+
+ final LocaleHelper localeHelper = application.getLocaleHelper();
+ initCountrySpinner(localeHelper);
+ initLanguageSpinner(localeHelper);
+
+ genderSpinner.setItemsArray(R.array.helloactivity_gender_list);
+ unitSpinner.setItemsArray(R.array.helloactivity_preferred_glucose_unit);
+ typeSpinner.setItemsArray(R.array.helloactivity_diabetes_type);
+
+ initStartButton();
+
+ showAnalyticsExplanationDialog();
+
+ Log.i("HelloActivity", "Setting screen name: hello");
+
+
+ }
+
+
+ private void initLanguageSpinner(final LocaleHelper localeHelper) {
+ localesWithTranslation = localeHelper.getLocalesWithTranslation(getResources());
+
+ List displayLanguages = new ArrayList<>(localesWithTranslation.size());
+ for (String language : localesWithTranslation) {
+ if (language.length() > 0) {
+ displayLanguages.add(localeHelper.getDisplayLanguage(language));
+ }
+ }
+
+ languageSpinner.setItemsArray(displayLanguages);
+
+ final Locale deviceLocale = localeHelper.getDeviceLocale();
+ String displayLanguage = localeHelper.getDisplayLanguage(deviceLocale.toString());
+
+ setSelection(displayLanguage, languageSpinner);
+ }
+
+ private void setSelection(final String label, final LabelledSpinner labelledSpinner) {
+ if (label != null) {
+ int position = ((ArrayAdapter) labelledSpinner.getSpinner().getAdapter()).getPosition(label);
+ labelledSpinner.setSelection(position);
+ }
+ }
+
+ private void initStartButton() {
+ final Drawable pinkArrow = ResourcesCompat.getDrawable(getResources(),
+ R.drawable.ic_navigate_next_pink_24px, null);
+ if (pinkArrow != null) {
+ pinkArrow.setBounds(0, 0, 60, 60);
+ startButton.setCompoundDrawables(null, null, pinkArrow, null);
+ }
+ }
+
+ private void initCountrySpinner(final LocaleHelper localeHelper) {
+ // Get countries list from locale
+ ArrayList countries = new ArrayList<>();
+ Locale[] locales = Locale.getAvailableLocales();
+
+ for (Locale locale : locales) {
+ String country = locale.getDisplayCountry();
+
+ if ((country.trim().length() > 0) && (!countries.contains(country))) {
+ countries.add(country);
+ }
+ }
+
+ Collections.sort(countries);
+
+ // Populate Spinners with array
+ countrySpinner.setItemsArray(countries);
+
+ // Get locale country name and set the spinner
+ String localCountry = localeHelper.getDeviceLocale().getDisplayCountry();
+
+ setSelection(localCountry, countrySpinner);
+ }
+
+ @OnClick(R.id.activity_hello_button_start)
+ void onStartClicked() {
+ presenter.onNextClicked(ageTextView.getText().toString(),
+ genderSpinner.getSpinner().getSelectedItem().toString(),
+ localesWithTranslation.get(languageSpinner.getSpinner().getSelectedItemPosition()),
+ countrySpinner.getSpinner().getSelectedItem().toString(),
+ typeSpinner.getSpinner().getSelectedItemPosition() + 1,
+ unitSpinner.getSpinner().getSelectedItem().toString());
+ }
+
+ @OnClick(R.id.helloactivity_textview_terms)
+ void onTermsAndConditionClick() {
+ ExternalLinkActivity.launch(
+ this,
+ getString(R.string.preferences_terms),
+ GlucosioExternalLinks.TERMS);
+ }
+
+ public void displayErrorWrongAge() {
+ //Why toast and not error in edit box or dialog
+ Toast.makeText(getApplicationContext(), getString(R.string.helloactivity_age_invalid), Toast.LENGTH_SHORT).show();
+ }
+
+ public void startMainView() {
+ Intent intent = new Intent(this, MainActivity.class);
+ startActivity(intent);
+ finish();
+ }
+
+ private void showAnalyticsExplanationDialog() {
+ new AlertDialog.Builder(this)
+ .setTitle(R.string.analytics_usage)
+ .setMessage(R.string.analytics_usage_overview)
+ .setCancelable(false)
+ .setNegativeButton(R.string.optout, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+ sharedPref.edit().putBoolean("pref_analytics_opt_in", false).apply();
+ }
+ })
+ .setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+ sharedPref.edit().putBoolean("pref_analytics_opt_in", true).apply();
+ GlucosioApplication application = (GlucosioApplication) getApplication();
+ Analytics analytics = application.getAnalytics();
+ analytics.reportScreen("Hello Activity");
+ }
+ })
+ .show();
+ }
+
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/MainActivity.java b/app/src/main/java/org/glucosio/android/activity/MainActivity.java
new file mode 100644
index 0000000..3d32a89
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/MainActivity.java
@@ -0,0 +1,796 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.Manifest;
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.app.DatePickerDialog;
+import android.app.Dialog;
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.graphics.Typeface;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.design.widget.AppBarLayout;
+import android.support.design.widget.BottomSheetBehavior;
+import android.support.design.widget.BottomSheetDialog;
+import android.support.design.widget.CoordinatorLayout;
+import android.support.design.widget.FloatingActionButton;
+import android.support.design.widget.Snackbar;
+import android.support.design.widget.TabLayout;
+import android.support.graphics.drawable.VectorDrawableCompat;
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.view.ViewPager;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.DatePicker;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.RadioButton;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.google.android.gms.appinvite.AppInviteInvitation;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+import com.mikepenz.materialdrawer.AccountHeaderBuilder;
+import com.mikepenz.materialdrawer.Drawer;
+import com.mikepenz.materialdrawer.DrawerBuilder;
+import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
+import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
+
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.adapter.HomePagerAdapter;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.presenter.ExportPresenter;
+import org.glucosio.android.presenter.MainPresenter;
+import org.glucosio.android.tools.LocaleHelper;
+import org.glucosio.android.view.ExportView;
+
+import java.util.Calendar;
+
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+
+public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener,
+ ActivityCompat.OnRequestPermissionsResultCallback, ExportView {
+
+ public static final String FROM_DATE_DIALOG_TAG = "fromDateDialog";
+ private static final String INTENT_EXTRA_DROPDOWN = "history_dropdown";
+ private static final int REQUEST_INVITE = 1;
+ private static final String INTENT_EXTRA_PAGER = "pager";
+ private static final int REQUEST_EXTERNAL_STORAGE = 1;
+ private static final String[] PERMISSIONS_STORAGE = {
+ Manifest.permission.READ_EXTERNAL_STORAGE,
+ Manifest.permission.WRITE_EXTERNAL_STORAGE
+ };
+ private BottomSheetBehavior bottomSheetBehavior;
+ private ExportPresenter exportPresenter;
+ private RadioButton exportRangeButton;
+ private HomePagerAdapter homePagerAdapter;
+ private MainPresenter presenter;
+ private ViewPager viewPager;
+ private BottomSheetDialog bottomSheetAddDialog;
+ private TextView exportDialogDateFrom;
+ private TextView exportDialogDateTo;
+ private View bottomSheetAddDialogView;
+ private TabLayout tabLayout;
+ private LocaleHelper localeHelper;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ GlucosioApplication application = (GlucosioApplication) getApplication();
+
+ initPresenters(application);
+ setContentView(R.layout.activity_main);
+
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+ tabLayout = findViewById(R.id.activity_main_tab_layout);
+ viewPager = findViewById(R.id.activity_main_pager);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(false);
+ getSupportActionBar().setElevation(0);
+ getSupportActionBar().setTitle("");
+ getSupportActionBar().setLogo(R.drawable.ic_logo);
+ }
+
+ homePagerAdapter = new HomePagerAdapter(getSupportFragmentManager(), getApplicationContext());
+
+ viewPager.setAdapter(homePagerAdapter);
+ tabLayout.setupWithViewPager(viewPager);
+ tabLayout.addOnTabSelectedListener(
+ new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
+ @Override
+ public void onTabSelected(TabLayout.Tab tab) {
+ super.onTabSelected(tab);
+ }
+ });
+
+ viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ if (position == 2) {
+ hideFabAnimation();
+ LinearLayout emptyLayout = findViewById(R.id.activity_main_empty_layout);
+ ViewPager pager = findViewById(R.id.activity_main_pager);
+ if (pager.getVisibility() == View.GONE) {
+ pager.setVisibility(View.VISIBLE);
+ emptyLayout.setVisibility(View.INVISIBLE);
+ }
+ } else {
+ showFabAnimation();
+ checkIfEmptyLayout();
+ }
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+
+ }
+ });
+
+ FloatingActionButton fabAddReading = findViewById(R.id.activity_main_fab_add_reading);
+ fabAddReading.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ bottomSheetAddDialog.show();
+ bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
+ }
+ });
+
+ bottomSheetAddDialog = new BottomSheetDialog(this);
+
+ // Add Nav Drawer
+ final PrimaryDrawerItem itemSettings = new PrimaryDrawerItem().withName(R.string.action_settings).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_settings_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemExport = new PrimaryDrawerItem().withName(R.string.sidebar_backup_export).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_backup_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemFeedback = new PrimaryDrawerItem().withName(R.string.menu_support).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_announcement_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemAbout = new PrimaryDrawerItem().withName(R.string.preferences_about_glucosio).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_info_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemInvite = new PrimaryDrawerItem().withName(R.string.action_invite).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_face_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemDonate = new PrimaryDrawerItem().withName(R.string.about_donate).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_favorite_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemA1C = new PrimaryDrawerItem().withName(R.string.activity_converter_title).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_calculator_a1c_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+ final PrimaryDrawerItem itemReminders = new PrimaryDrawerItem().withName(R.string.activity_reminders_title).withIcon(VectorDrawableCompat.create(getResources(), R.drawable.ic_alarm_grey_24dp, null)).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
+
+ DrawerBuilder drawerBuilder = new DrawerBuilder()
+ .withActivity(this)
+ .withTranslucentStatusBar(false)
+ .withToolbar(toolbar)
+ .withActionBarDrawerToggle(true)
+ .withAccountHeader(new AccountHeaderBuilder()
+ .withActivity(this)
+ .withHeaderBackground(R.drawable.drawer_header)
+ .build()
+ )
+ .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
+ @Override
+ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
+ if (drawerItem.equals(itemSettings)) {
+ // Settings
+ openPreferences();
+ } else if (drawerItem.equals(itemAbout)) {
+ // About
+ startAboutActivity();
+ } else if (drawerItem.equals(itemFeedback)) {
+ // Feedback
+ openSupportDialog();
+ } else if (drawerItem.equals(itemInvite)) {
+ // Invite
+ showInviteDialog();
+ } else if (drawerItem.equals(itemExport)) {
+ // Export
+ startExportActivity();
+ } else if (drawerItem.equals(itemDonate)) {
+ // Donate
+ openDonateIntent();
+ } else if (drawerItem.equals(itemA1C)) {
+ openA1CCalculator();
+ } else if (drawerItem.equals(itemReminders)) {
+ openRemindersActivity();
+ }
+ return false;
+ }
+ });
+
+ if (isPlayServicesAvailable()) {
+ drawerBuilder.addDrawerItems(
+ itemA1C,
+ itemReminders,
+ itemExport,
+ itemSettings,
+ itemFeedback,
+ itemAbout,
+ itemDonate,
+ itemInvite
+ )
+ .withSelectedItem(-1)
+ .build();
+ } else {
+ drawerBuilder.addDrawerItems(
+ itemA1C,
+ itemReminders,
+ itemExport,
+ itemSettings,
+ itemFeedback,
+ itemAbout,
+ itemDonate
+ )
+ .withSelectedItem(-1)
+ .build();
+ }
+
+ // Restore pager position
+ Bundle b = getIntent().getExtras();
+ if (b != null) {
+ viewPager.setCurrentItem(b.getInt("pager"));
+ }
+
+ checkIfEmptyLayout();
+ bottomSheetAddDialog.setContentView(bottomSheetAddDialogView);
+ bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetAddDialogView.getParent());
+ bottomSheetBehavior.setHideable(false);
+
+ Analytics analytics = application.getAnalytics();
+ Log.i("MainActivity", "Setting screen name: " + "main");
+ analytics.reportScreen("Main Activity");
+ }
+
+ private void openRemindersActivity() {
+ Intent intent = new Intent(this, RemindersActivity.class);
+ startActivity(intent);
+ }
+
+ private void initPresenters(GlucosioApplication application) {
+ final DatabaseHandler dbHandler = application.getDBHandler();
+ localeHelper = new LocaleHelper();
+ presenter = new MainPresenter(this, dbHandler);
+ exportPresenter = new ExportPresenter(this, this, dbHandler);
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+
+
+ @Override
+ public void onExportStarted(int numberOfItemsToExport) {
+ showExportedSnackBar(numberOfItemsToExport); // TODO: 09/09/16 Instead of calling this method, move logic to this callback ?
+ Log.d("Activity", "onExportStarted(): you might want to track this event");
+ }
+
+ @Override
+ public void onNoItemsToExport() {
+ showNoReadingsSnackBar(); // TODO: 09/09/16 Instead of calling this method, move logic to this callback ?
+ Log.e("Activity", "onNoItemsToExport(): you might want to track this event");
+ }
+
+ @Override
+ public void onExportFinish(@NonNull Uri uri) {
+ showShareDialog(uri); // TODO: 09/09/16 Instead of calling this method, move logic to this callback ?
+ Log.e("Activity", "onExportFinish(): you might want to track this event");
+ }
+
+ @Override
+ public void onExportError() {
+ showExportError(); // TODO: 09/09/16 Instead of calling this method, move logic to this callback ?
+ Log.e("Activity", "onExportError(): you might want to track this event");
+ }
+
+ @Override
+ public void requestStoragePermission() {
+ ActivityCompat.requestPermissions(
+ this,
+ PERMISSIONS_STORAGE,
+ REQUEST_EXTERNAL_STORAGE
+ );
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+
+ if (requestCode == REQUEST_EXTERNAL_STORAGE) {
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
+ grantResults[1] == PackageManager.PERMISSION_GRANTED) {
+ // Storage permissions granted, show the CSV dialog again
+ showExportCsvDialog();
+ } else {
+ showExportPermissionError();
+ }
+ }
+ }
+
+ private void openA1CCalculator() {
+ Intent calculatorIntent = new Intent(this, A1cCalculatorActivity.class);
+ startActivity(calculatorIntent);
+ }
+
+ private void openDonateIntent() {
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.glucosio.org/donate/"));
+ startActivity(browserIntent);
+ }
+
+ public void startExportActivity() {
+ openBackupDialog();
+ }
+
+ private void startAboutActivity() {
+ Intent intent = new Intent(this, AboutActivity.class);
+ startActivity(intent);
+ }
+
+ public void startHelloActivity() {
+ Intent intent = new Intent(this, HelloActivity.class);
+ startActivity(intent);
+ finish();
+ }
+
+ public void openPreferences() {
+ Intent intent = new Intent(this, PreferencesActivity.class);
+ startActivity(intent);
+ finishActivity();
+ }
+
+ public void finishActivity() {
+ // dismiss dialog if still expanded
+ bottomSheetAddDialog.dismiss();
+ // then close activity
+ finish();
+ }
+
+ public void onGlucoseFabClicked(View v) {
+ openNewAddActivity(AddGlucoseActivity.class);
+ }
+
+ public void onKetoneFabClicked(View v) {
+ openNewAddActivity(AddKetoneActivity.class);
+ }
+
+ public void onPressureFabClicked(View v) {
+ openNewAddActivity(AddPressureActivity.class);
+ }
+
+ public void onHB1ACFabClicked(View v) {
+ openNewAddActivity(AddA1CActivity.class);
+ }
+
+ public void onCholesterolFabClicked(View v) {
+ openNewAddActivity(AddCholesterolActivity.class);
+ }
+
+ public void onWeightFabClicked(View v) {
+ openNewAddActivity(AddWeightActivity.class);
+ }
+
+ private void openNewAddActivity(Class> activity) {
+ Intent intent = new Intent(this, activity);
+ // Pass pager position to open it again later
+ Bundle b = new Bundle();
+ b.putInt(INTENT_EXTRA_PAGER, viewPager.getCurrentItem());
+ b.putInt(INTENT_EXTRA_DROPDOWN, homePagerAdapter.getHistoryFragment().getHistoryDropdownPosition());
+ intent.putExtras(b);
+ startActivity(intent);
+ finishActivity();
+ }
+
+ public void openSupportDialog() {
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle(getResources().getString(R.string.menu_support_title));
+ builder.setItems(getResources().getStringArray(R.array.menu_support_options), new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+
+ // Forum
+ String url = "http://community.glucosio.org/";
+ Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ i.setPackage("com.android.chrome");
+ try {
+ startActivity(i);
+ } catch (ActivityNotFoundException e) {
+ // Chrome is probably not installed
+ // Try with the default browser
+ i.setPackage(null);
+ startActivity(i);
+ }
+ }
+ });
+ builder.show();
+ }
+
+ public void openBackupDialog() {
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle(getResources().getString(R.string.activity_main_dialog_backup_export_title));
+ builder.setItems(getResources().getStringArray(R.array.menu_backup_options), new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ // Backup to Google Drive has been removed because it crashes
+ // It needs to be re-implemented using non-deprecated API
+ if (which == 0) {
+ // Export to CSV
+ showExportCsvDialog();
+ }
+ }
+ });
+ builder.show();
+ }
+
+ public void showExportCsvDialog() {
+ if (hasStoragePermissions()) {
+ final Dialog exportDialog = new Dialog(MainActivity.this, R.style.GlucosioTheme);
+
+ WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
+ lp.copyFrom(exportDialog.getWindow().getAttributes());
+ lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
+ lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
+ exportDialog.setContentView(R.layout.dialog_export);
+ exportDialog.getWindow().setAttributes(lp);
+ exportDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
+ exportDialog.getWindow().setDimAmount(0.5f);
+ exportDialog.show();
+
+ exportDialogDateFrom = exportDialog.findViewById(R.id.activity_export_date_from);
+ exportDialogDateTo = exportDialog.findViewById(R.id.activity_export_date_to);
+ exportRangeButton = exportDialog.findViewById(R.id.activity_export_range);
+ final RadioButton exportAllButton = exportDialog.findViewById(R.id.activity_export_all);
+ final TextView exportButton = exportDialog.findViewById(R.id.dialog_export_add);
+ final TextView cancelButton = exportDialog.findViewById(R.id.dialog_export_cancel);
+
+ exportRangeButton.setChecked(true);
+
+ exportDialogDateFrom.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Calendar now = Calendar.getInstance();
+ DatePickerDialog dpd = new DatePickerDialog(
+ MainActivity.this,
+ MainActivity.this,
+ now.get(Calendar.YEAR),
+ now.get(Calendar.MONTH),
+ now.get(Calendar.DAY_OF_MONTH)
+ );
+ dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
+ // We use this tag to determine which date the user has edited
+ dpd.getDatePicker().setTag(FROM_DATE_DIALOG_TAG);
+ dpd.show();
+ }
+ });
+
+ exportDialogDateTo.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Calendar now = Calendar.getInstance();
+ DatePickerDialog dpd = new DatePickerDialog(
+ MainActivity.this,
+ MainActivity.this,
+ now.get(Calendar.YEAR),
+ now.get(Calendar.MONTH),
+ now.get(Calendar.DAY_OF_MONTH)
+ );
+ dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
+ dpd.show();
+ }
+ });
+
+ exportRangeButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ boolean isChecked = exportRangeButton.isChecked();
+ exportDialogDateFrom.setEnabled(true);
+ exportDialogDateTo.setEnabled(true);
+ exportAllButton.setChecked(!isChecked);
+ }
+ });
+
+ exportAllButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ boolean isChecked = exportAllButton.isChecked();
+ exportDialogDateFrom.setEnabled(false);
+ exportDialogDateTo.setEnabled(false);
+ exportRangeButton.setChecked(!isChecked);
+ exportButton.setEnabled(true);
+ }
+ });
+
+ exportButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ if (validateExportDialog()) {
+ exportPresenter.onExportClicked(exportAllButton.isChecked());
+ exportDialog.dismiss();
+ } else {
+ showSnackBar(getResources().getString(R.string.dialog_error), Snackbar.LENGTH_LONG);
+ }
+ }
+ });
+
+ cancelButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ exportDialog.dismiss();
+ }
+ });
+ }
+ }
+
+ private boolean validateExportDialog() {
+ String dateTo = exportDialogDateTo.getText().toString();
+ String dateFrom = exportDialogDateFrom.getText().toString();
+ return !exportRangeButton.isChecked() || !(TextUtils.isEmpty(dateTo) || TextUtils.isEmpty(dateFrom));
+ }
+
+ public CoordinatorLayout getFabView() {
+ return (CoordinatorLayout) findViewById(R.id.activity_main_coordinator_layout);
+ }
+
+ public void reloadFragmentAdapter() {
+ homePagerAdapter.notifyDataSetChanged();
+ }
+
+ public void turnOffToolbarScrolling() {
+ Toolbar mToolbar = findViewById(R.id.activity_main_toolbar);
+ AppBarLayout appBarLayout = findViewById(R.id.activity_main_appbar_layout);
+
+ //turn off scrolling
+ AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
+ toolbarLayoutParams.setScrollFlags(0);
+ mToolbar.setLayoutParams(toolbarLayoutParams);
+
+ CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
+ appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
+ appBarLayout.setLayoutParams(appBarLayoutParams);
+ }
+
+ public void turnOnToolbarScrolling() {
+ Toolbar mToolbar = findViewById(R.id.activity_main_toolbar);
+ AppBarLayout appBarLayout = findViewById(R.id.activity_main_appbar_layout);
+
+ //turn on scrolling
+ AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
+ toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
+ mToolbar.setLayoutParams(toolbarLayoutParams);
+
+ CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
+ appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
+ appBarLayout.setLayoutParams(appBarLayoutParams);
+ }
+
+ public Toolbar getToolbar() {
+ return (Toolbar) findViewById(R.id.activity_main_toolbar);
+ }
+
+ public LocaleHelper getLocaleHelper() {
+ return localeHelper;
+ }
+
+ private void hideFabAnimation() {
+ final View fab = findViewById(R.id.activity_main_fab_add_reading);
+ fab.animate()
+ .translationY(-5)
+ .alpha(0.0f)
+ .setListener(new AnimatorListenerAdapter() {
+ @Override
+ public void onAnimationEnd(Animator animation) {
+ super.onAnimationEnd(animation);
+ fab.setVisibility(View.INVISIBLE);
+ }
+ });
+ }
+
+ private void showFabAnimation() {
+ final View fab = findViewById(R.id.activity_main_fab_add_reading);
+ if (fab.getVisibility() == View.INVISIBLE) {
+ // Prepare the View for the animation
+ fab.setVisibility(View.VISIBLE);
+ fab.setAlpha(0.0f);
+
+ fab.animate()
+ .alpha(1f)
+ .setListener(new AnimatorListenerAdapter() {
+ @Override
+ public void onAnimationEnd(Animator animation) {
+ super.onAnimationEnd(animation);
+ fab.setVisibility(View.VISIBLE);
+ }
+ });
+ } else {
+ // do nothing
+ // probably swiping from OVERVIEW to HISTORY tab
+ }
+ }
+
+ public void showInviteDialog() {
+ Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
+ .setMessage(getString(R.string.invitation_message))
+ .setCallToActionText(getString(R.string.invitation_cta))
+ .build();
+ startActivityForResult(intent, REQUEST_INVITE);
+ }
+
+ public void checkIfEmptyLayout() {
+ LinearLayout emptyLayout = findViewById(R.id.activity_main_empty_layout);
+ ViewPager pager = findViewById(R.id.activity_main_pager);
+
+ if (presenter.isdbEmpty()) {
+ pager.setVisibility(View.GONE);
+ tabLayout.setVisibility(View.GONE);
+ emptyLayout.setVisibility(View.VISIBLE);
+
+ bottomSheetAddDialogView = getLayoutInflater().inflate(R.layout.fragment_add_bottom_dialog_disabled, null);
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ if (getResources().getConfiguration().orientation == 1) {
+ // If Portrait choose vertical curved line
+ ImageView arrow = findViewById(R.id.activity_main_arrow);
+ arrow.setBackground(getResources().getDrawable(R.drawable.curved_line_vertical));
+ } else {
+ // Else choose horizontal one
+ ImageView arrow = findViewById(R.id.activity_main_arrow);
+ arrow.setBackground((getResources().getDrawable(R.drawable.curved_line_horizontal)));
+ }
+ }
+ } else {
+ pager.setVisibility(View.VISIBLE);
+ emptyLayout.setVisibility(View.GONE);
+ bottomSheetAddDialogView = getLayoutInflater().inflate(R.layout.fragment_add_bottom_dialog, null);
+ }
+ }
+
+ public void showExportedSnackBar(int nReadings) {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.activity_export_snackbar_1) + " " + nReadings + " " + getString(R.string.activity_export_snackbar_2), Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void showNoReadingsSnackBar() {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.activity_export_no_readings_snackbar), Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void showExportError() {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.activity_export_issue_generic), Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void showExportPermissionError() {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, getString(R.string.activity_export_issue_permissions), Snackbar.LENGTH_SHORT).show();
+ }
+
+ private void showSnackBar(String text, int lengthLong) {
+ View rootLayout = findViewById(android.R.id.content);
+ Snackbar.make(rootLayout, text, lengthLong).show();
+ }
+
+ public void showShareDialog(Uri uri) {
+ Intent shareIntent = new Intent(Intent.ACTION_SEND);
+ shareIntent.setData(uri);
+ shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+ shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+ shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
+ shareIntent.setType("*/*");
+ startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.share_using)));
+ }
+
+ @Override
+ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
+ // Check which dialog set the date
+ int monthToShow = monthOfYear + 1;
+ if (String.valueOf(view.getTag()).equals(FROM_DATE_DIALOG_TAG)) {
+ exportPresenter.setFrom(year, monthToShow, dayOfMonth);
+
+ String date = +dayOfMonth + "/" + monthToShow + "/" + year;
+ exportDialogDateFrom.setText(date);
+ } else {
+ exportPresenter.setTo(year, monthToShow, dayOfMonth);
+
+ String date = +dayOfMonth + "/" + monthToShow + "/" + year;
+ exportDialogDateTo.setText(date);
+ }
+ }
+
+ private boolean isPlayServicesAvailable() {
+ GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
+ int status = googleAPI.isGooglePlayServicesAvailable(getApplicationContext());
+ if (status == ConnectionResult.SUCCESS)
+ return true;
+ else {
+ Log.d("STATUS", "Error connecting with Google Play services. Code: " + String.valueOf(status));
+ return false;
+ }
+ }
+
+ public void onA1cInfoClicked(View view) {
+ new AlertDialog.Builder(MainActivity.this)
+ .setMessage(getString(R.string.overview_hb1ac_info))
+ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ // continue with delete
+ }
+ })
+ .show();
+
+ addA1cAnalyticsEvent();
+ }
+
+ private void addA1cAnalyticsEvent() {
+ Analytics analytics = ((GlucosioApplication) getApplication()).getAnalytics();
+ analytics.reportAction("A1C", "A1C disclaimer opened");
+ }
+
+ private boolean hasStoragePermissions() {
+ // Check if we have write permission
+ int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+ Log.i("Glucosio", "Storage permissions granted.");
+
+ if (permission == PackageManager.PERMISSION_DENIED) {
+ requestStoragePermission();
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Check the device to make sure it has the Google Play Services APK. If
+ * it doesn't, display a dialog that allows users to download the APK from
+ * the Google Play Store or enable it in the device's system settings.
+ */
+ private boolean checkPlayServices() {
+ GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
+ int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
+ if (resultCode != ConnectionResult.SUCCESS) {
+ if (apiAvailability.isUserResolvableError(resultCode)) {
+ apiAvailability.getErrorDialog(this, resultCode, 9000)
+ .show();
+ } else {
+ Log.i("Glucosio", "This device is not supported.");
+ showErrorDialogPlayServices();
+ }
+ return false;
+ }
+ return true;
+ }
+
+ private void showErrorDialogPlayServices() {
+ Toast.makeText(getApplicationContext(), R.string.activity_main_error_play_services, Toast.LENGTH_SHORT).show();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java b/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java
new file mode 100644
index 0000000..588b99c
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java
@@ -0,0 +1,517 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.activity;
+
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.preference.EditTextPreference;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceFragment;
+import android.preference.SwitchPreference;
+import android.support.annotation.NonNull;
+import android.support.annotation.StringRes;
+import android.support.v7.app.ActionBar;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.text.InputFilter;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.MenuItem;
+import android.widget.EditText;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.analytics.Analytics;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.User;
+import org.glucosio.android.tools.GlucoseRanges;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.InputFilterMinMax;
+import org.glucosio.android.tools.LocaleHelper;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
+
+public class PreferencesActivity extends AppCompatActivity {
+
+ @NonNull
+ private static String[] getEntryValues(List list) {
+ String[] result = new String[list.size()];
+ return list.toArray(result);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.preferences);
+
+ getFragmentManager().beginTransaction()
+ .replace(R.id.preferencesFrame, new MyPreferenceFragment()).commit();
+
+ ActionBar supportActionBar = getSupportActionBar();
+ if (supportActionBar != null) {
+ supportActionBar.setDisplayHomeAsUpEnabled(true);
+ supportActionBar.setTitle(getString(R.string.action_settings));
+ }
+
+ // Obtain the Analytics shared Tracker instance.
+ GlucosioApplication application = (GlucosioApplication) getApplication();
+ Analytics analytics = application.getAnalytics();
+ Log.i("PreferencesActivity", "Setting screen name: preferences");
+ analytics.reportScreen("Preferences");
+ }
+
+ @Override
+ public void onBackPressed() {
+ Intent intent = new Intent(this, MainActivity.class);
+ startActivity(intent);
+ finish();
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == android.R.id.home) {
+ Intent intent = new Intent(this, MainActivity.class);
+ startActivity(intent);
+ finish();
+ }
+ return true;
+ }
+
+ @Override
+ protected void attachBaseContext(Context newBase) {
+ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
+ }
+
+ public static class MyPreferenceFragment extends PreferenceFragment {
+ private DatabaseHandler dB;
+ private User user;
+ private ListPreference languagePref;
+ private ListPreference countryPref;
+ private ListPreference genderPref;
+ private ListPreference diabetesTypePref;
+ private ListPreference unitPrefGlucose;
+ private ListPreference unitPrefA1c;
+ private ListPreference unitPrefWeight;
+ private ListPreference rangePref;
+ private EditText ageEditText;
+ private EditText minEditText;
+ private EditText maxEditText;
+ private EditTextPreference agePref;
+ private EditTextPreference minRangePref;
+ private EditTextPreference maxRangePref;
+ private SwitchPreference dyslexiaModePref;
+ private SwitchPreference freestyleLibrePref;
+ private SwitchPreference analyticsOptInPref;
+ private User updatedUser;
+ private LocaleHelper localeHelper;
+
+
+ @Override
+ public void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.preferences);
+
+ final GlucosioApplication app = (GlucosioApplication) getActivity().getApplicationContext();
+ dB = app.getDBHandler();
+ localeHelper = app.getLocaleHelper();
+ user = dB.getUser(1);
+ updatedUser = new User(user);
+ agePref = (EditTextPreference) findPreference("pref_age");
+ countryPref = (ListPreference) findPreference("pref_country");
+ languagePref = (ListPreference) findPreference("pref_language");
+ genderPref = (ListPreference) findPreference("pref_gender");
+ diabetesTypePref = (ListPreference) findPreference("pref_diabetes_type");
+ unitPrefGlucose = (ListPreference) findPreference("pref_unit_glucose");
+ unitPrefA1c = (ListPreference) findPreference("pref_unit_a1c");
+ unitPrefWeight = (ListPreference) findPreference("pref_unit_weight");
+ rangePref = (ListPreference) findPreference("pref_range");
+ minRangePref = (EditTextPreference) findPreference("pref_range_min");
+ maxRangePref = (EditTextPreference) findPreference("pref_range_max");
+ dyslexiaModePref = (SwitchPreference) findPreference("pref_font_dyslexia");
+ freestyleLibrePref = (SwitchPreference) findPreference("pref_freestyle_libre");
+ analyticsOptInPref = (SwitchPreference) findPreference("pref_analytics_opt_in");
+
+ agePref.setDefaultValue(user.getAge());
+ countryPref.setValue(user.getCountry());
+ genderPref.setValue(user.getGender());
+ diabetesTypePref.setValue(String.valueOf(user.getD_type()));
+ unitPrefGlucose.setValue(getGlucoseUnitValue(user.getPreferred_unit()));
+ unitPrefA1c.setValue(getA1CUnitValue(user.getPreferred_unit_a1c()));
+ unitPrefWeight.setValue(getUnitWeight(user.getPreferred_unit_weight()));
+ rangePref.setValue(user.getPreferred_range());
+
+ if (Constants.Units.MG_DL.equals(user.getPreferred_unit())) {
+ maxRangePref.setDefaultValue(user.getCustom_range_max());
+ minRangePref.setDefaultValue(user.getCustom_range_min());
+ } else {
+ maxRangePref.setDefaultValue(GlucosioConverter.glucoseToMmolL(user.getCustom_range_max()));
+ minRangePref.setDefaultValue(GlucosioConverter.glucoseToMmolL(user.getCustom_range_min()));
+ }
+
+ if (!"custom".equals(rangePref.getValue())) {
+ minRangePref.setEnabled(false);
+ maxRangePref.setEnabled(false);
+ } else {
+ minRangePref.setEnabled(true);
+ maxRangePref.setEnabled(true);
+ }
+
+ final Preference aboutPref = findPreference("about_settings");
+ countryPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ updatedUser.setCountry(newValue.toString());
+ updateDB();
+ return false;
+ }
+ });
+ agePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (newValue.toString().trim().equals("")) {
+ return false;
+ }
+ updatedUser.setAge(Integer.parseInt(newValue.toString()));
+ updateDB();
+ return true;
+ }
+ });
+ genderPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ updatedUser.setGender(newValue.toString());
+ updateDB();
+ return true;
+ }
+ });
+ diabetesTypePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ String[] typesArray = getResources().getStringArray(R.array.helloactivity_diabetes_type);
+ String selectedType = newValue.toString();
+
+ if (selectedType.equals(typesArray[0])) {
+ updatedUser.setD_type(1);
+ updateDB();
+ } else if (selectedType.equals(typesArray[1])) {
+ updatedUser.setD_type(2);
+ updateDB();
+ } else if (selectedType.equals(typesArray[2])) {
+ updatedUser.setD_type(3);
+ updateDB();
+ } else {
+ updatedUser.setD_type(4);
+ updateDB();
+ }
+
+ return true;
+ }
+ });
+ unitPrefGlucose.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ String preferredUnit = getResources().getString(R.string.helloactivity_spinner_preferred_glucose_unit_1).equals(newValue.toString()) ?
+ Constants.Units.MG_DL :
+ Constants.Units.MMOL_L;
+ updatedUser.setPreferred_unit(preferredUnit);
+ updateDB();
+ return true;
+ }
+ });
+ unitPrefA1c.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (newValue.toString().equals(getResources().getString(R.string.preferences_spinner_preferred_a1c_unit_1))) {
+ updatedUser.setPreferred_unit_a1c("percentage");
+ } else {
+ updatedUser.setPreferred_unit_a1c("mmol/mol");
+ }
+ updateDB();
+ return true;
+ }
+ });
+ unitPrefWeight.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (newValue.toString().equals(getResources().getString(R.string.preferences_spinner_preferred_weight_unit_1))) {
+ updatedUser.setPreferred_unit_weight("kilograms");
+ } else {
+ updatedUser.setPreferred_unit_weight("pounds");
+ }
+ updateDB();
+ return true;
+ }
+ });
+ rangePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ String selectedPreset = newValue.toString();
+ updatedUser.setPreferred_range(selectedPreset);
+
+ // look up the min/max values of the selected preset
+ if (!selectedPreset.equals("Custom range")) {
+ int rangeMin = GlucoseRanges.getPresetMin(selectedPreset);
+ int rangeMax = GlucoseRanges.getPresetMax(selectedPreset);
+ // min/max ranges are stored in mg/dl format
+ updatedUser.setCustom_range_min(rangeMin);
+ updatedUser.setCustom_range_max(rangeMax);
+ }
+
+ updateDB();
+ return true;
+ }
+ });
+
+ minRangePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ minEditText.setText("");
+ return false;
+ }
+ });
+
+ minRangePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (TextUtils.isEmpty(newValue.toString().trim())) {
+ return false;
+ }
+ double glucoseDouble = ReadingTools.safeParseDouble(newValue.toString());
+ if (user.getPreferred_unit().equals(Constants.Units.MG_DL)) {
+ updatedUser.setCustom_range_min(glucoseDouble);
+ } else {
+ updatedUser.setCustom_range_min(GlucosioConverter.glucoseToMgDl(glucoseDouble));
+ }
+ updateDB();
+ return true;
+ }
+ });
+
+ maxRangePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ maxEditText.setText("");
+ return false;
+ }
+ });
+ maxRangePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (TextUtils.isEmpty(newValue.toString().trim())) {
+ return false;
+ }
+ if (user.getPreferred_unit().equals(Constants.Units.MG_DL)) {
+ updatedUser.setCustom_range_max(ReadingTools.safeParseDouble(newValue.toString()));
+ } else {
+ updatedUser.setCustom_range_max(GlucosioConverter.glucoseToMgDl(ReadingTools.safeParseDouble(newValue.toString())));
+ }
+ updateDB();
+ return true;
+ }
+ });
+ dyslexiaModePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ // EXPERIMENTAL PREFERENCE
+ // Display Alert
+ showExperimentalDialog(true);
+ return true;
+ }
+ });
+ freestyleLibrePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if (!((SwitchPreference) preference).isChecked()) {
+ // EXPERIMENTAL PREFERENCE
+ // Display Alert
+ showExperimentalDialog(false);
+ }
+ return true;
+ }
+ });
+ analyticsOptInPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ return true;
+ }
+ });
+ ageEditText = agePref.getEditText();
+ minEditText = minRangePref.getEditText();
+ maxEditText = maxRangePref.getEditText();
+
+ ageEditText.setFilters(new InputFilter[]{new InputFilterMinMax(1, 110)});
+
+ // Get countries list from locale
+ ArrayList countriesArray = new ArrayList<>();
+ Locale[] locales = Locale.getAvailableLocales();
+
+ for (Locale locale : locales) {
+ String country = locale.getDisplayCountry();
+ if (country.trim().length() > 0 && !countriesArray.contains(country)) {
+ countriesArray.add(country);
+ }
+ }
+ Collections.sort(countriesArray);
+
+ CharSequence[] countries = countriesArray.toArray(new CharSequence[countriesArray.size()]);
+ countryPref.setEntryValues(countries);
+ countryPref.setEntries(countries);
+
+ initLanguagePreference();
+
+ updateDB();
+
+ aboutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ Intent aboutActivity = new Intent(getActivity(), AboutActivity.class);
+ getActivity().startActivity(aboutActivity);
+ return false;
+ }
+ });
+
+ }
+
+ private String getA1CUnitValue(final String a1CUnit) {
+ @StringRes int unitResId = "percentage".equals(a1CUnit) ?
+ R.string.preferences_spinner_preferred_a1c_unit_1 :
+ R.string.preferences_spinner_preferred_a1c_unit_2;
+ return getResources().getString(unitResId);
+ }
+
+ private String getGlucoseUnitValue(final String glucoseUnit) {
+ @StringRes int unitResId = Constants.Units.MG_DL.equals(glucoseUnit) ?
+ R.string.helloactivity_spinner_preferred_glucose_unit_1 :
+ R.string.helloactivity_spinner_preferred_glucose_unit_2;
+ return getResources().getString(unitResId);
+ }
+
+ private String getUnitWeight(final String unit_weight) {
+ @StringRes int unitResId = "kilograms".equals(unit_weight) ?
+ R.string.preferences_spinner_preferred_weight_unit_1 :
+ R.string.preferences_spinner_preferred_weight_unit_2;
+ return getResources().getString(unitResId);
+ }
+
+ private void initLanguagePreference() {
+ List valuesLanguages = localeHelper.getLocalesWithTranslation(getResources());
+
+ List displayLanguages = new ArrayList<>(valuesLanguages.size());
+ for (String language : valuesLanguages) {
+ if (language.length() > 0) {
+ displayLanguages.add(localeHelper.getDisplayLanguage(language));
+ }
+ }
+
+ languagePref.setEntryValues(getEntryValues(valuesLanguages));
+ languagePref.setEntries(getEntryValues(displayLanguages));
+
+ String languageValue = user.getPreferred_language();
+ if (languageValue != null) {
+ languagePref.setValue(languageValue);
+ String displayLanguage = localeHelper.getDisplayLanguage(languageValue);
+ languagePref.setSummary(displayLanguage);
+ }
+
+ languagePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+
+ String language = (String) newValue;
+ updatedUser.setPreferred_language(language);
+ languagePref.setSummary(localeHelper.getDisplayLanguage(language));
+ languagePref.setValue(language);
+
+ updateDB();
+
+ localeHelper.updateLanguage(getActivity(), language);
+ getActivity().recreate();
+
+ return true;
+ }
+ });
+ }
+
+ private void updateDB() {
+ dB.updateUser(updatedUser);
+
+ countryPref.setSummary(user.getCountry());
+ agePref.setSummary(String.valueOf(user.getAge()));
+ genderPref.setSummary(String.valueOf(user.getGender()));
+ diabetesTypePref.setSummary(getResources().getStringArray(R.array.helloactivity_diabetes_type)[user.getD_type() - 1]);
+ unitPrefGlucose.setSummary(getGlucoseUnitValue(user.getPreferred_unit()));
+ unitPrefA1c.setSummary(getA1CUnitValue(user.getPreferred_unit_a1c()));
+ unitPrefWeight.setSummary(getUnitWeight(user.getPreferred_unit_weight()));
+ rangePref.setSummary(user.getPreferred_range());
+
+ if (Constants.Units.MG_DL.equals(user.getPreferred_unit())) {
+ minRangePref.setSummary(String.valueOf(user.getCustom_range_min()));
+ maxRangePref.setSummary(String.valueOf(user.getCustom_range_max()));
+ } else {
+ minRangePref.setSummary(String.valueOf(GlucosioConverter.glucoseToMmolL(user.getCustom_range_min())));
+ maxRangePref.setSummary(String.valueOf(GlucosioConverter.glucoseToMmolL(user.getCustom_range_max())));
+ }
+
+ if (!user.getPreferred_range().equals("Custom range")) {
+ minRangePref.setEnabled(false);
+ maxRangePref.setEnabled(false);
+ } else {
+ minRangePref.setEnabled(true);
+ maxRangePref.setEnabled(true);
+ }
+ }
+
+ private void showExperimentalDialog(final boolean restartRequired) {
+ new AlertDialog.Builder(getActivity())
+ .setTitle(getResources().getString(R.string.preferences_experimental_title))
+ .setMessage(R.string.preferences_experimental)
+ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ if (restartRequired) {
+ rebootApp();
+ }
+ }
+ })
+ .show();
+ }
+
+ private void rebootApp() {
+ Intent mStartActivity = new Intent(getActivity().getApplicationContext(), MainActivity.class);
+ int mPendingIntentId = 123456;
+ PendingIntent mPendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+ AlarmManager mgr = (AlarmManager) getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
+ mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+ System.exit(0);
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/RemindersActivity.java b/app/src/main/java/org/glucosio/android/activity/RemindersActivity.java
new file mode 100644
index 0000000..66df8ea
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/RemindersActivity.java
@@ -0,0 +1,175 @@
+package org.glucosio.android.activity;
+
+import android.app.TimePickerDialog;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.support.design.widget.BottomSheetDialog;
+import android.support.design.widget.FloatingActionButton;
+import android.support.design.widget.Snackbar;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.text.InputType;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.widget.ListView;
+import android.widget.TableLayout;
+import android.widget.TimePicker;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.Reminder;
+import org.glucosio.android.presenter.RemindersPresenter;
+import org.glucosio.android.tools.AnimationTools;
+
+import java.util.Calendar;
+
+public class RemindersActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
+
+ private FloatingActionButton addFab;
+ private RemindersPresenter presenter;
+ private ListView listView;
+ private String label;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_reminders);
+ presenter = new RemindersPresenter(this);
+
+ Toolbar toolbar = findViewById(R.id.activity_main_toolbar);
+
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setElevation(2);
+ getSupportActionBar().setTitle(getResources().getString(R.string.activity_reminders_title));
+ }
+
+ addFab = findViewById(R.id.activity_reminders_fab_add);
+
+ addFab.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(final View view) {
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(RemindersActivity.this);
+ builder.setTitle(R.string.activity_reminder_add_label);
+
+ // Set up the input
+ final EditText input = new EditText(RemindersActivity.this);
+ // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
+ input.setInputType(InputType.TYPE_CLASS_TEXT);
+ // Set EditText margin
+ TableLayout.LayoutParams params = new TableLayout.LayoutParams();
+ params.setMargins(16, 16, 16, 16);
+ input.setLayoutParams(params);
+ builder.setView(input);
+
+ // Set up the buttons
+ builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ label = input.getText().toString();
+ if (label.isEmpty()) {
+ showEmptyErrorMessage(view);
+ } else {
+ openTimePicker();
+ }
+ }
+ });
+ builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ dialog.cancel();
+ }
+ });
+
+ builder.show();
+ }
+ });
+
+ listView = findViewById(R.id.activity_reminders_listview);
+ listView.setEmptyView(findViewById(R.id.activity_reminders_listview_empty));
+ listView.setAdapter(presenter.getAdapter());
+ addFab.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ AnimationTools.startCircularReveal(addFab);
+ }
+ }, 600);
+ }
+
+ private void openTimePicker() {
+ // Open Time Picker on FAB click
+ boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(getApplicationContext());
+ Calendar cal = presenter.getCalendar();
+
+ TimePickerDialog tpd = new TimePickerDialog(
+ RemindersActivity.this,
+ RemindersActivity.this,
+ cal.get(Calendar.HOUR_OF_DAY),
+ cal.get(Calendar.MINUTE),
+ is24HourFormat);
+ tpd.show();
+ }
+
+ public void updateReminder(Reminder reminder) {
+ presenter.updateReminder(reminder);
+ presenter.saveReminders();
+ }
+
+ public void updateRemindersList() {
+ listView.setAdapter(presenter.getAdapter());
+ listView.invalidate();
+ }
+
+ private void showEmptyErrorMessage(View view) {
+ Snackbar.make(view, R.string.activity_reminder_error_empty, Snackbar.LENGTH_SHORT).show();
+ }
+
+ @Override
+ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
+ cal.set(Calendar.MINUTE, minute);
+ // Id is HOURS+MINUTES to avoid duplicates
+ String concatenatedId = hourOfDay + "" + minute;
+ // Metric is always glucose until I write support for other metrics so...
+ // TODO: Add Reminders for other metrics
+ // Also oneTime is always set to false until I implement one time alarms
+ // TODO: Implement one time alarms
+ presenter.addReminder(Long.parseLong(concatenatedId), cal.getTime(), label, "glucose", false, true);
+ }
+
+ public void showDuplicateError() {
+ View parentLayout = findViewById(R.id.activity_reminders_root_view);
+ Snackbar.make(parentLayout, R.string.activitiy_reminders_error_duplicate, Snackbar.LENGTH_SHORT).show();
+ }
+
+ public void showBottomSheetDialog(final long id) {
+ final BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
+ View sheetView = getLayoutInflater().inflate(R.layout.fragment_reminders_bottom_sheet, null);
+ LinearLayout delete = sheetView.findViewById(R.id.fragment_history_bottom_sheet_delete);
+ delete.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ presenter.deleteReminder(id);
+ updateRemindersList();
+ mBottomSheetDialog.dismiss();
+ }
+ });
+
+ mBottomSheetDialog.setContentView(sheetView);
+ mBottomSheetDialog.show();
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem menuItem) {
+ if (menuItem.getItemId() == android.R.id.home) {
+ presenter.saveReminders();
+ finish();
+ }
+ return super.onOptionsItemSelected(menuItem);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/activity/SplashActivity.java b/app/src/main/java/org/glucosio/android/activity/SplashActivity.java
new file mode 100644
index 0000000..77432a2
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/activity/SplashActivity.java
@@ -0,0 +1,17 @@
+package org.glucosio.android.activity;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+
+public class SplashActivity extends AppCompatActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Intent intent = new Intent(this, MainActivity.class);
+ startActivity(intent);
+ finish();
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/glucosio/android/adapter/A1cEstimateAdapter.java b/app/src/main/java/org/glucosio/android/adapter/A1cEstimateAdapter.java
new file mode 100644
index 0000000..43f047a
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/A1cEstimateAdapter.java
@@ -0,0 +1,76 @@
+package org.glucosio.android.adapter;
+
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.R;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.object.A1cEstimate;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.NumberFormatUtils;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.text.NumberFormat;
+import java.util.List;
+
+public class A1cEstimateAdapter extends ArrayAdapter {
+ private final DatabaseHandler databaseHandler;
+ private final NumberFormat numberFormat = NumberFormatUtils.createDefaultNumberFormat();
+
+ public A1cEstimateAdapter(Context context, int resource, List items) {
+ super(context, resource, items);
+ databaseHandler = new DatabaseHandler(context);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+
+ View v = convertView;
+
+ if (v == null) {
+ LayoutInflater vi;
+ vi = LayoutInflater.from(getContext());
+ v = vi.inflate(R.layout.dialog_a1c_item, parent, false);
+ }
+
+ A1cEstimate p = getItem(position);
+
+ if (p != null) {
+ TextView value = v.findViewById(R.id.dialog_a1c_item_value);
+ TextView month = v.findViewById(R.id.dialog_a1c_item_month);
+ TextView glucoseAverage = v.findViewById(R.id.dialog_a1c_item_glucose_value);
+
+ if (value != null) {
+ if ("percentage".equals(databaseHandler.getUser(1).getPreferred_unit_a1c())) {
+ String stringValue = p.getValue() + " %";
+ value.setText(stringValue);
+ } else {
+ String stringValue = GlucosioConverter.a1cNgspToIfcc(p.getValue()) + " mmol/mol";
+ value.setText(stringValue);
+ }
+ }
+
+ if (month != null) {
+ month.setText(p.getMonth());
+ }
+
+ if (glucoseAverage != null) {
+ if (Constants.Units.MG_DL.equals(databaseHandler.getUser(1).getPreferred_unit())) {
+ glucoseAverage.setText(getContext().getString(R.string.mg_dL_value, p.getGlucoseAverage()));
+ } else {
+ double mmol = GlucosioConverter.glucoseToMgDl(ReadingTools.safeParseDouble(p.getGlucoseAverage()));
+ String reading = numberFormat.format(mmol);
+ glucoseAverage.setText(getContext().getString(R.string.mmol_L_value, reading));
+ }
+ }
+ }
+
+ return v;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/adapter/AssistantAdapter.java b/app/src/main/java/org/glucosio/android/adapter/AssistantAdapter.java
new file mode 100644
index 0000000..2c95659
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/AssistantAdapter.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.adapter;
+
+import android.content.res.Resources;
+import android.support.annotation.NonNull;
+import android.support.v7.widget.AppCompatButton;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import org.glucosio.android.R;
+import org.glucosio.android.object.ActionTip;
+import org.glucosio.android.presenter.AssistantPresenter;
+
+import java.util.List;
+
+public class AssistantAdapter extends RecyclerView.Adapter {
+ private List actionTips;
+ private AssistantPresenter presenter;
+ private Resources res;
+
+ // Provide a suitable constructor (depends on the kind of dataset)
+ public AssistantAdapter(AssistantPresenter assistantPresenter, final Resources resources, List tips) {
+ this.res = resources;
+ this.presenter = assistantPresenter;
+ this.actionTips = tips;
+ }
+
+ // Create new views (invoked by the layout manager)
+ @Override
+ public AssistantAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ // create a new view
+ View v = LayoutInflater.from(parent.getContext())
+ .inflate(R.layout.fragment_assistant_item, parent, false);
+
+ return new ViewHolder(v);
+ }
+
+ // Replace the contents of a view (invoked by the layout manager)
+ @Override
+ public void onBindViewHolder(ViewHolder holder, int position) {
+ TextView actionTipTitle = holder.mView.findViewById(R.id.fragment_assistant_item_title);
+ TextView actionTipDescription = holder.mView.findViewById(R.id.fragment_assistant_item_description);
+ AppCompatButton actionTipAction = holder.mView.findViewById(R.id.fragment_assistant_item_action);
+ actionTipTitle.setText(actionTips.get(position).getTipTitle());
+ actionTipDescription.setText(actionTips.get(position).getTipDescription());
+ actionTipAction.setText(actionTips.get(position).getTipAction());
+ String actionTipTitleString = actionTips.get(position).getTipTitle();
+
+ View.OnClickListener actionListener = getActionListener(actionTipTitleString);
+ actionTipAction.setOnClickListener(actionListener);
+ }
+
+ @NonNull
+ private View.OnClickListener getActionListener(String actionTipTitleString) {
+ View.OnClickListener actionListener;
+ //TODO: OOP or at least switch
+ if (actionTipTitleString.equals(res.getString(R.string.assistant_feedback_title))) {
+ actionListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ presenter.userSupportAsked();
+
+ }
+ };
+ } else if (actionTipTitleString.equals(res.getString(R.string.assistant_export_title))) {
+ actionListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ presenter.userAskedExport();
+ }
+ };
+ } else if (actionTipTitleString.equals(res.getString(R.string.assistant_calculator_a1c_title))) {
+ actionListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ presenter.userAskedA1CCalculator();
+ }
+ };
+ } else {
+ actionListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ presenter.userAskedAddReading();
+ }
+ };
+ }
+ return actionListener;
+ }
+
+ // Return the size of your dataset (invoked by the layout manager)
+ @Override
+ public int getItemCount() {
+ return actionTips.size();
+ }
+
+ // Provide a reference to the views for each data item
+ // Complex data items may need more than one view per item, and
+ // you provide access to all the views for a data item in a view holder
+ static class ViewHolder extends RecyclerView.ViewHolder {
+ // each data item is just a string in this case
+ View mView;
+
+ ViewHolder(View v) {
+ super(v);
+ mView = v;
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/adapter/BackupAdapter.java b/app/src/main/java/org/glucosio/android/adapter/BackupAdapter.java
new file mode 100644
index 0000000..2120bf2
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/BackupAdapter.java
@@ -0,0 +1,92 @@
+package org.glucosio.android.adapter;
+
+
+import android.app.Dialog;
+import android.content.Context;
+import android.support.annotation.NonNull;
+import android.text.format.Formatter;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.TextView;
+
+import com.google.android.gms.drive.DriveId;
+
+import org.glucosio.android.R;
+import org.glucosio.android.activity.BackupActivity;
+import org.glucosio.android.object.GlucosioBackup;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.List;
+
+public class BackupAdapter extends ArrayAdapter {
+
+ private Context context;
+ private FormatDateTime formatDateTime;
+
+ public BackupAdapter(Context context, int resource, List items) {
+ super(context, resource, items);
+ this.context = context;
+ formatDateTime = new FormatDateTime(context);
+ }
+
+ @Override
+ @NonNull
+ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
+ View v = convertView;
+
+ if (v == null) {
+ LayoutInflater vi;
+ vi = LayoutInflater.from(getContext());
+ v = vi.inflate(R.layout.activity_backup_drive_restore_item, parent, false);
+ }
+
+ GlucosioBackup p = getItem(position);
+ if (p != null) {
+ final DriveId driveId = p.getDriveId();
+ final String modified = formatDateTime.formatDate(p.getModifiedDate());
+ final String size = Formatter.formatFileSize(getContext(), p.getBackupSize());
+
+ TextView modifiedTextView = v.findViewById(R.id.item_history_time);
+ TextView typeTextView = v.findViewById(R.id.item_history_type);
+ modifiedTextView.setText(modified);
+ typeTextView.setText(size);
+
+ v.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ // Show custom dialog
+ final Dialog dialog = new Dialog(context);
+ dialog.setContentView(R.layout.dialog_backup_restore);
+ TextView createdTextView = dialog.findViewById(R.id.dialog_backup_restore_created);
+ TextView sizeTextView = dialog.findViewById(R.id.dialog_backup_restore_size);
+ Button restoreButton = dialog.findViewById(R.id.dialog_backup_restore_button_restore);
+ Button cancelButton = dialog.findViewById(R.id.dialog_backup_restore_button_cancel);
+
+ createdTextView.setText(modified);
+ sizeTextView.setText(size);
+
+ restoreButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ ((BackupActivity) context).downloadFromDrive(driveId.asDriveFile());
+ }
+ });
+
+ cancelButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ dialog.dismiss();
+ }
+ });
+
+ dialog.show();
+ }
+ });
+ }
+
+ return v;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/adapter/HistoryAdapter.java b/app/src/main/java/org/glucosio/android/adapter/HistoryAdapter.java
new file mode 100644
index 0000000..13f0d1f
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/HistoryAdapter.java
@@ -0,0 +1,281 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.adapter;
+
+import android.content.Context;
+import android.support.v4.content.ContextCompat;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.R;
+import org.glucosio.android.presenter.HistoryPresenter;
+import org.glucosio.android.tools.GlucoseRanges;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.NumberFormatUtils;
+
+import java.text.NumberFormat;
+import java.util.Collections;
+import java.util.List;
+
+public class HistoryAdapter extends RecyclerView.Adapter {
+ private final int metricId;
+ private final Context mContext;
+ private final HistoryPresenter presenter;
+ private final NumberFormat numberFormat = NumberFormatUtils.createDefaultNumberFormat();
+ private List weightDataTime;
+ private List weightIdArray;
+ private List weightReadingArray;
+ private List ketoneDataTimeArray;
+ private List ketoneReadingArray;
+ private List ketoneIdArray;
+ private List pressureDateTimeArray;
+ private List pressureMinArray;
+ private List pressureMaxArray;
+ private List pressureIdArray;
+ private List cholesterolDateTimeArray;
+ private List cholesterolHDLArray;
+ private List cholesterolLDLArray;
+ private List cholesterolTotalArray;
+ private List cholesterolIdArray;
+ private List hb1acDateTimeArray;
+ private List hb1acReadingArray;
+ private List hb1acIdArray;
+ private List glucoseIdArray;
+ private List glucoseNotes;
+ private List glucoseReadingArray;
+ private List glucoseDateTime;
+ private List glucoseReadingType;
+
+ // Provide a suitable constructor (depends on the kind of dataset)
+ public HistoryAdapter(Context context, HistoryPresenter presenter, int metricId) {
+ this.mContext = context;
+ this.presenter = presenter;
+ this.metricId = metricId;
+
+ switch (metricId) {
+ // Glucose
+ case 0:
+ // Reverse ListView order to display latest items first
+ Collections.addAll(presenter.getGlucoseReading());
+ Collections.addAll(presenter.getGlucoseDateTime());
+ Collections.addAll(presenter.getGlucoseReadingType());
+ Collections.addAll(presenter.getGlucoseId());
+ Collections.addAll(presenter.getGlucoseNotes());
+ glucoseReadingArray = presenter.getGlucoseReading();
+ glucoseDateTime = presenter.getGlucoseDateTime();
+ glucoseReadingType = presenter.getGlucoseReadingType();
+ glucoseIdArray = presenter.getGlucoseId();
+ glucoseNotes = presenter.getGlucoseNotes();
+ break;
+ // HB1AC
+ case 1:
+ hb1acIdArray = presenter.getHB1ACId();
+ hb1acReadingArray = presenter.getHB1ACReading();
+ hb1acDateTimeArray = presenter.getHB1ACDateTime();
+ break;
+ // Cholesterol
+ case 2:
+ cholesterolIdArray = presenter.getCholesterolId();
+ cholesterolTotalArray = presenter.getTotalCholesterolReading();
+ cholesterolLDLArray = presenter.getLDLCholesterolReading();
+ cholesterolHDLArray = presenter.getHDLCholesterolReading();
+ cholesterolDateTimeArray = presenter.getCholesterolDateTime();
+ break;
+ // Pressure
+ case 3:
+ pressureIdArray = presenter.getPressureId();
+ pressureMaxArray = presenter.getMaxPressureReading();
+ pressureMinArray = presenter.getMinPressureReading();
+ pressureDateTimeArray = presenter.getPressureDateTime();
+ break;
+ //Ketones
+ case 4:
+ ketoneIdArray = presenter.getKetoneId();
+ ketoneReadingArray = presenter.getKetoneReading();
+ ketoneDataTimeArray = presenter.getKetoneDateTime();
+ break;
+ // Weight
+ case 5:
+ weightIdArray = presenter.getWeightId();
+ weightReadingArray = presenter.getWeightReadings();
+ weightDataTime = presenter.getWeightDateTime();
+ break;
+ }
+ }
+
+ // Create new views (invoked by the layout manager)
+ @Override
+ public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
+ int viewType) {
+
+ // create a new view
+ View v = LayoutInflater.from(parent.getContext())
+ .inflate(R.layout.fragment_history_item, parent, false);
+
+ return new ViewHolder(v);
+ }
+
+ // Replace the contents of a view (invoked by the layout manager)
+ @Override
+ public void onBindViewHolder(ViewHolder holder, int position) {
+ TextView readingTextView = holder.mView.findViewById(R.id.item_history_reading);
+ TextView datetimeTextView = holder.mView.findViewById(R.id.item_history_time);
+ TextView typeTextView = holder.mView.findViewById(R.id.item_history_type);
+ TextView idTextView = holder.mView.findViewById(R.id.item_history_id);
+ TextView notesTextView = holder.mView.findViewById(R.id.item_history_notes);
+
+ switch (metricId) {
+ // Glucose
+ case 0:
+ idTextView.setText(glucoseIdArray.get(position).toString());
+ GlucoseRanges ranges = new GlucoseRanges(mContext);
+ String color = ranges.colorFromReading(glucoseReadingArray.get(position));
+
+ if (Constants.Units.MG_DL.equals(presenter.getUnitMeasuerement())) {
+ double glucoseReading = glucoseReadingArray.get(position);
+ String reading = numberFormat.format(glucoseReading);
+ readingTextView.setText(mContext.getString(R.string.mg_dL_value, reading));
+ } else {
+ double mmol = GlucosioConverter.glucoseToMmolL(glucoseReadingArray.get(position));
+ String reading = numberFormat.format(mmol);
+ readingTextView.setText(mContext.getString(R.string.mmol_L_value, reading));
+ }
+
+ readingTextView.setTextColor(ranges.stringToColor(color));
+ datetimeTextView.setText(presenter.convertDate(glucoseDateTime.get(position)));
+ typeTextView.setText(glucoseReadingType.get(position));
+ String notes = glucoseNotes.get(position);
+ if (!notes.isEmpty()) {
+ notesTextView.setText(glucoseNotes.get(position));
+ notesTextView.setVisibility(View.VISIBLE);
+ } else {
+ notesTextView.setText("");
+ notesTextView.setVisibility(View.GONE);
+ }
+ break;
+ // A1C
+ case 1:
+ idTextView.setText(hb1acIdArray.get(position).toString());
+ if ("percentage".equals(presenter.getA1cUnitMeasurement())) {
+ readingTextView.setText(numberFormat.format(hb1acReadingArray.get(position)) + " %");
+ } else {
+ double ifcc = GlucosioConverter.a1cNgspToIfcc(hb1acReadingArray.get(position));
+ String reading = numberFormat.format(ifcc);
+ readingTextView.setText(mContext.getString(R.string.mmol_mol_value, reading));
+ }
+ datetimeTextView.setText(presenter.convertDate(hb1acDateTimeArray.get(position)));
+ typeTextView.setText("");
+ typeTextView.setVisibility(View.GONE);
+ readingTextView.setTextColor(ContextCompat.getColor(mContext, R.color.glucosio_text_dark));
+ break;
+ // Cholesterol
+ case 2:
+ idTextView.setText(cholesterolIdArray.get(position).toString());
+ String reading = numberFormat.format(cholesterolTotalArray.get(position));
+ readingTextView.setText(mContext.getString(R.string.mg_dL_value, reading));
+ datetimeTextView.setText(presenter.convertDate(cholesterolDateTimeArray.get(position)));
+ typeTextView.setText("LDL: " + numberFormat.format(cholesterolLDLArray.get(position)) +
+ " - " + "HDL: " + numberFormat.format(cholesterolHDLArray.get(position)));
+ readingTextView.setTextColor(ContextCompat.getColor(mContext, R.color.glucosio_text_dark));
+ break;
+ // Pressure
+ case 3:
+ idTextView.setText(pressureIdArray.get(position).toString());
+ readingTextView.setText(numberFormat.format(pressureMaxArray.get(position)) + "/" +
+ numberFormat.format(pressureMinArray.get(position)) + " mm/Hg");
+ datetimeTextView.setText(presenter.convertDate(pressureDateTimeArray.get(position)));
+ typeTextView.setText("");
+ typeTextView.setVisibility(View.GONE);
+ readingTextView.setTextColor(ContextCompat.getColor(mContext, R.color.glucosio_text_dark));
+ break;
+ //Ketones
+ case 4:
+ idTextView.setText(ketoneIdArray.get(position).toString());
+ readingTextView.setText(numberFormat.format(ketoneReadingArray.get(position)) + " mmol");
+ datetimeTextView.setText(presenter.convertDate(ketoneDataTimeArray.get(position)));
+ typeTextView.setText("");
+ typeTextView.setVisibility(View.GONE);
+ readingTextView.setTextColor(ContextCompat.getColor(mContext, R.color.glucosio_text_dark));
+ break;
+ // Weight
+ case 5:
+ idTextView.setText(weightIdArray.get(position).toString());
+
+ //TODO: localise
+ if ("kilograms".equals(presenter.getWeightUnitMeasurement())) {
+ readingTextView.setText(numberFormat.format(weightReadingArray.get(position)) + " kg");
+ } else {
+ double lb = GlucosioConverter.kgToLb(weightReadingArray.get(position));
+ readingTextView.setText(numberFormat.format(lb) + " lbs");
+ }
+
+ datetimeTextView.setText(presenter.convertDate(weightDataTime.get(position)));
+ typeTextView.setText("");
+ typeTextView.setVisibility(View.GONE);
+ readingTextView.setTextColor(ContextCompat.getColor(mContext, R.color.glucosio_text_dark));
+ break;
+ }
+ }
+
+ // Return the size of your dataset (invoked by the layout manager)
+ @Override
+ public int getItemCount() {
+ switch (metricId) {
+ // Glucose
+ case 0:
+ return glucoseIdArray.size();
+ // HB1AC
+ case 1:
+ return hb1acIdArray.size();
+ // Cholesterol
+ case 2:
+ return cholesterolIdArray.size();
+ // Pressure
+ case 3:
+ return pressureIdArray.size();
+ //Ketones
+ case 4:
+ return ketoneIdArray.size();
+ // Weight
+ case 5:
+ return weightIdArray.size();
+ default:
+ return 0;
+ }
+ }
+
+ // Provide a reference to the views for each data item
+ // Complex data items may need more than one view per item, and
+ // you provide access to all the views for a data item in a view holder
+ public static class ViewHolder extends RecyclerView.ViewHolder {
+ // each data item is just a string in this case
+ public View mView;
+
+ public ViewHolder(View v) {
+ super(v);
+ mView = v;
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/adapter/HomePagerAdapter.java b/app/src/main/java/org/glucosio/android/adapter/HomePagerAdapter.java
new file mode 100644
index 0000000..2ff070d
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/HomePagerAdapter.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.adapter;
+
+import android.content.Context;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentPagerAdapter;
+
+import org.glucosio.android.R;
+import org.glucosio.android.fragment.AssistantFragment;
+import org.glucosio.android.fragment.HistoryFragment;
+import org.glucosio.android.fragment.OverviewFragment;
+
+public class HomePagerAdapter extends FragmentPagerAdapter {
+
+ private Context mContext;
+ private OverviewFragment overviewFragment;
+ private HistoryFragment historyFragment;
+ private AssistantFragment assistantFragment;
+
+
+ public HomePagerAdapter(FragmentManager fm, Context context) {
+ super(fm);
+ this.mContext = context;
+ overviewFragment = OverviewFragment.newInstance();
+ historyFragment = HistoryFragment.newInstance();
+ assistantFragment = new AssistantFragment();
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ switch (position) {
+ case 0:
+ return overviewFragment;
+ case 1:
+ return historyFragment;
+ default:
+ return assistantFragment;
+ }
+ }
+
+ @Override
+ public int getCount() {
+ return 3;
+ }
+
+ // Workaround to refresh views with notifyDataSetChanged()
+ public int getItemPosition(Object object) {
+ return POSITION_NONE;
+ }
+
+ @Override
+ public CharSequence getPageTitle(int position) {
+ switch (position) {
+ case 0:
+ return mContext.getString(R.string.tab_overview);
+ case 1:
+ return mContext.getString(R.string.tab_history);
+ default:
+ return mContext.getString(R.string.assistant);
+ }
+ }
+
+
+ public OverviewFragment getOverviewFragment() {
+ return overviewFragment;
+ }
+
+ public void setOverviewFragment(OverviewFragment overviewFragment) {
+ this.overviewFragment = overviewFragment;
+ }
+
+ public HistoryFragment getHistoryFragment() {
+ return historyFragment;
+ }
+
+ public void setHistoryFragment(HistoryFragment historyFragment) {
+ this.historyFragment = historyFragment;
+ }
+
+ public AssistantFragment getAssistantFragment() {
+ return assistantFragment;
+ }
+
+ public void setAssistantFragment(AssistantFragment assistantFragment) {
+ this.assistantFragment = assistantFragment;
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/glucosio/android/adapter/RemindersAdapter.java b/app/src/main/java/org/glucosio/android/adapter/RemindersAdapter.java
new file mode 100644
index 0000000..de3a905
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/adapter/RemindersAdapter.java
@@ -0,0 +1,93 @@
+package org.glucosio.android.adapter;
+
+import android.content.Context;
+import android.support.annotation.NonNull;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.CompoundButton;
+import android.widget.LinearLayout;
+import android.widget.Switch;
+import android.widget.TextView;
+
+import org.glucosio.android.R;
+import org.glucosio.android.activity.RemindersActivity;
+import org.glucosio.android.db.Reminder;
+import org.glucosio.android.tools.FormatDateTime;
+
+import java.util.Calendar;
+import java.util.List;
+
+public class RemindersAdapter extends ArrayAdapter {
+ private Context context;
+ private List items;
+ private Calendar calendar;
+ private FormatDateTime formatDateTime;
+
+ public RemindersAdapter(Context context, int textViewResourceId) {
+ super(context, textViewResourceId);
+ }
+
+ public RemindersAdapter(Context context, int resource, List items) {
+ super(context, resource, items);
+ this.context = context;
+ this.items = items;
+ calendar = Calendar.getInstance();
+ formatDateTime = new FormatDateTime(context);
+ }
+
+ @Override
+ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
+ View v = convertView;
+ if (v == null) {
+ LayoutInflater vi;
+ vi = LayoutInflater.from(getContext());
+ v = vi.inflate(R.layout.activity_reminder_item, parent, false);
+ }
+
+ LinearLayout rootView = v.findViewById(R.id.activity_reminders_root_view);
+ TextView timeTextView = v.findViewById(R.id.activity_reminders_item_time);
+ TextView labelTextView = v.findViewById(R.id.activity_reminders_label);
+ Switch activeSwitch = v.findViewById(R.id.activity_reminders_item_enabled);
+ final Reminder reminder = items.get(position);
+ final long reminderId = reminder.getId();
+
+ calendar.setTime(reminder.getAlarmTime());
+ timeTextView.setText(formatDateTime.getTime(calendar));
+ labelTextView.setText(reminder.getLabel());
+ activeSwitch.setChecked(reminder.isActive());
+
+ activeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ Reminder updatedReminder = new Reminder(reminder.getId(), reminder.getAlarmTime(), reminder.getLabel(), reminder.getMetric(),
+ reminder.isOneTime(), reminder.isActive());
+ updatedReminder.setActive(b);
+ updateReminder(updatedReminder);
+ }
+ });
+
+ rootView.setOnLongClickListener(new View.OnLongClickListener() {
+ @Override
+ public boolean onLongClick(View view) {
+ showBottomSheetMenu(reminderId);
+ return true;
+ }
+ });
+
+ return v;
+ }
+
+ private void showBottomSheetMenu(long id) {
+ ((RemindersActivity) context).showBottomSheetDialog(id);
+ }
+
+ private void updateReminder(Reminder reminder) {
+ // Should be always true.
+ // I HOPE...
+ if (context instanceof RemindersActivity) {
+ ((RemindersActivity) context).updateReminder(reminder);
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/analytics/Analytics.java b/app/src/main/java/org/glucosio/android/analytics/Analytics.java
new file mode 100644
index 0000000..f1edd8b
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/analytics/Analytics.java
@@ -0,0 +1,12 @@
+package org.glucosio.android.analytics;
+
+import android.content.Context;
+import android.support.annotation.NonNull;
+
+public interface Analytics {
+ void init(@NonNull final Context context);
+
+ void reportScreen(@NonNull final String name);
+
+ void reportAction(@NonNull final String category, @NonNull final String name);
+}
diff --git a/app/src/main/java/org/glucosio/android/analytics/GlucosioGoogleAnalytics.java b/app/src/main/java/org/glucosio/android/analytics/GlucosioGoogleAnalytics.java
new file mode 100644
index 0000000..e354873
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/analytics/GlucosioGoogleAnalytics.java
@@ -0,0 +1,51 @@
+package org.glucosio.android.analytics;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+import android.support.annotation.NonNull;
+import android.util.Log;
+
+import com.google.android.gms.analytics.GoogleAnalytics;
+import com.google.android.gms.analytics.HitBuilders;
+import com.google.android.gms.analytics.Tracker;
+
+import org.glucosio.android.BuildConfig;
+
+public class GlucosioGoogleAnalytics implements Analytics {
+ private Tracker mTracker;
+
+ @Override
+ public void init(@NonNull final Context context) {
+ GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
+ // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
+ mTracker = analytics.newTracker(BuildConfig.GOOGLE_ANALYTICS_TRACKER);
+ mTracker.enableAdvertisingIdCollection(true);
+
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
+ boolean enabled = sharedPref.getBoolean("pref_analytics_opt_in", false);
+
+ if (BuildConfig.DEBUG) {
+ GoogleAnalytics.getInstance(context).setAppOptOut(true);
+ Log.i("Glucosio", "DEBUG BUILD: ANALYTICS IS DISABLED");
+ } else {
+ GoogleAnalytics.getInstance(context).setAppOptOut(!enabled);
+ }
+ }
+
+ @Override
+ public void reportScreen(@NonNull String name) {
+ // No need to check flag since we set opt out and Google Analytics will ignore sending
+ mTracker.setScreenName(name);
+ mTracker.send(new HitBuilders.ScreenViewBuilder().build());
+ }
+
+ @Override
+ public void reportAction(@NonNull String category, @NonNull String name) {
+ // No need to check flag since we set opt out and Google Analytics will ignore sending
+ mTracker.send(new HitBuilders.EventBuilder()
+ .setCategory(category)
+ .setAction(name)
+ .build());
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/backup/Backup.java b/app/src/main/java/org/glucosio/android/backup/Backup.java
new file mode 100644
index 0000000..843ea21
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/backup/Backup.java
@@ -0,0 +1,16 @@
+package org.glucosio.android.backup;
+
+import android.app.Activity;
+import android.support.annotation.NonNull;
+
+import com.google.android.gms.common.api.GoogleApiClient;
+
+public interface Backup {
+ void init(@NonNull final Activity activity);
+
+ void start();
+
+ void stop();
+
+ GoogleApiClient getClient();
+}
diff --git a/app/src/main/java/org/glucosio/android/backup/GoogleDriveBackup.java b/app/src/main/java/org/glucosio/android/backup/GoogleDriveBackup.java
new file mode 100644
index 0000000..19f3de2
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/backup/GoogleDriveBackup.java
@@ -0,0 +1,91 @@
+package org.glucosio.android.backup;
+
+import android.app.Activity;
+import android.content.IntentSender;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.util.Log;
+
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+import com.google.android.gms.common.api.GoogleApiClient;
+import com.google.android.gms.drive.Drive;
+import com.google.firebase.crash.FirebaseCrash;
+
+import java.lang.ref.WeakReference;
+
+public class GoogleDriveBackup implements Backup, GoogleApiClient.OnConnectionFailedListener {
+ @Nullable
+ private GoogleApiClient googleApiClient;
+
+ @Nullable
+ private WeakReference activityRef;
+
+
+ @Override
+ public void init(@NonNull final Activity activity) {
+ this.activityRef = new WeakReference<>(activity);
+
+ googleApiClient = new GoogleApiClient.Builder(activity)
+ .addApi(Drive.API)
+ .addScope(Drive.SCOPE_FILE)
+ .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
+ @Override
+ public void onConnected(Bundle bundle) {
+ // Do nothing
+ }
+
+ @Override
+ public void onConnectionSuspended(int i) {
+
+ }
+ })
+ .addOnConnectionFailedListener(this)
+ .build();
+
+ }
+
+ @Override
+ public GoogleApiClient getClient() {
+ return googleApiClient;
+ }
+
+ @Override
+ public void start() {
+ if (googleApiClient != null) {
+ googleApiClient.connect();
+ } else {
+ throw new IllegalStateException("You should call init before start");
+ }
+ }
+
+ @Override
+ public void stop() {
+ if (googleApiClient != null) {
+ googleApiClient.disconnect();
+ } else {
+ throw new IllegalStateException("You should call init before start");
+ }
+ }
+
+ @Override
+ public void onConnectionFailed(@NonNull final ConnectionResult result) {
+ Log.i("Connection Failed", "GoogleApiClient connection failed: " + result.toString());
+
+ if (result.hasResolution() && activityRef != null && activityRef.get() != null) {
+ Activity a = activityRef.get();
+ // show the localized error dialog.
+ try {
+ result.startResolutionForResult(a, 1);
+ } catch (IntentSender.SendIntentException e) {
+ FirebaseCrash.log("Drive connection failed");
+ FirebaseCrash.report(e);
+ e.printStackTrace();
+ GoogleApiAvailability.getInstance().getErrorDialog(a, result.getErrorCode(), 0).show();
+ }
+ } else {
+ Log.d("error", "cannot resolve connection issue");
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/CholesterolReading.java b/app/src/main/java/org/glucosio/android/db/CholesterolReading.java
new file mode 100644
index 0000000..42a0b79
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/CholesterolReading.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class CholesterolReading extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private double totalReading;
+ private double LDLReading;
+ private double HDLReading;
+ private Date created;
+
+ public CholesterolReading() {
+ }
+
+ public CholesterolReading(double totalReading, double LDLReading, double HDLReading, Date created) {
+ // mg/dL
+ // 0-200
+ this.totalReading = totalReading;
+ this.LDLReading = LDLReading;
+ this.HDLReading = HDLReading;
+ this.created = created;
+ }
+
+ public double getTotalReading() {
+ return totalReading;
+ }
+
+ public void setTotalReading(double totalReading) {
+ this.totalReading = totalReading;
+ }
+
+ public double getLDLReading() {
+ return LDLReading;
+ }
+
+ public void setLDLReading(double LDLReading) {
+ this.LDLReading = LDLReading;
+ }
+
+ public double getHDLReading() {
+ return HDLReading;
+ }
+
+ public void setHDLReading(double HDLReading) {
+ this.HDLReading = HDLReading;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java b/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java
new file mode 100644
index 0000000..c1592cd
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java
@@ -0,0 +1,942 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import android.content.Context;
+
+import net.danlew.android.joda.JodaTimeAndroid;
+
+import org.joda.time.DateTime;
+import org.joda.time.Months;
+import org.joda.time.Weeks;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import io.realm.Realm;
+import io.realm.RealmConfiguration;
+import io.realm.RealmResults;
+import io.realm.Sort;
+
+public class DatabaseHandler {
+
+ private static RealmConfiguration mRealmConfig;
+ private Context mContext;
+ private Realm realm;
+
+ public DatabaseHandler(Context context) {
+ this.mContext = context;
+ Realm.init(context);
+ this.realm = getNewRealmInstance();
+ }
+
+ public Realm getNewRealmInstance() {
+ if (mRealmConfig == null) {
+ mRealmConfig = new RealmConfiguration.Builder()
+ .schemaVersion(5)
+ .migration(new Migration())
+ .build();
+ }
+ return Realm.getInstance(mRealmConfig); // Automatically run migration if needed
+ }
+
+ public Realm getRealmInstance() {
+ return realm;
+ }
+
+ public void addUser(User user) {
+ realm.beginTransaction();
+ realm.copyToRealm(user);
+ realm.commitTransaction();
+ }
+
+ public User getUser(long id) {
+ return realm.where(User.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public User getUser(Realm realm, long id) {
+ return realm.where(User.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void updateUser(User user) {
+ realm.beginTransaction();
+ realm.copyToRealmOrUpdate(user);
+ realm.commitTransaction();
+ }
+
+ public boolean addReminder(Reminder reminder) {
+ // Check for duplicates first
+ if (getReminder(reminder.getId()) == null) {
+ realm.beginTransaction();
+ realm.copyToRealm(reminder);
+ realm.commitTransaction();
+ return true;
+ }
+
+ return false;
+ }
+
+ public void updateReminder(Reminder reminder) {
+ realm.beginTransaction();
+ realm.copyToRealmOrUpdate(reminder);
+ realm.commitTransaction();
+ }
+
+ private void deleteReminder(Reminder reminder) {
+ realm.beginTransaction();
+ reminder.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public void deleteReminder(long id) {
+ deleteReminder(getReminder(id));
+ }
+
+ public boolean areRemindersActive() {
+ RealmResults activeRemindersList =
+ realm.where(Reminder.class)
+ .equalTo("active", true)
+ .findAll();
+
+ return !activeRemindersList.isEmpty();
+ }
+
+ public Reminder getReminder(long id) {
+ return realm.where(Reminder.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public List getReminders() {
+ RealmResults results =
+ realm.where(Reminder.class)
+ .sort("alarmTime", Sort.DESCENDING)
+ .findAll();
+ return new ArrayList<>(results);
+ }
+
+ public List getRemindersDatesAsArray() {
+ List readings = getReminders();
+ ArrayList datesArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ Date reading;
+ Reminder reminder = readings.get(i);
+ reading = reminder.getAlarmTime();
+ datesArray.add(reading);
+ }
+ return datesArray;
+ }
+
+ public List getRemindersDatesStringAsArray() {
+ List readings = getReminders();
+ ArrayList datesArray = new ArrayList<>(readings.size());
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ Reminder reminder = readings.get(i);
+ reading = inputFormat.format(reminder.getAlarmTime());
+ datesArray.add(reading);
+ }
+
+ return datesArray;
+ }
+
+ public boolean addGlucoseReading(GlucoseReading reading) {
+ // generate record Id
+ String id = generateIdFromDate(reading.getCreated(), reading.getId());
+
+ // Check for duplicates
+ if (getGlucoseReadingById(Long.parseLong(id)) != null) {
+ return false;
+ } else {
+ realm.beginTransaction();
+ reading.setId(Long.parseLong(id));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ return true;
+ }
+ }
+
+ private String generateIdFromDate(Date created, long readingId) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(created);
+ int year = calendar.get(Calendar.YEAR);
+ int month = calendar.get(Calendar.MONTH);
+ int day = calendar.get(Calendar.DAY_OF_MONTH);
+ int hours = calendar.get(Calendar.HOUR_OF_DAY);
+ int minutes = calendar.get(Calendar.MINUTE);
+ return "" + year + month + day + hours + minutes + readingId;
+ }
+
+ public void addNGlucoseReadings(int n) {
+ for (int i = 0; i < n; i++) {
+ Calendar calendar = Calendar.getInstance();
+ GlucoseReading gReading = new GlucoseReading(50 + i, "Debug reading", calendar.getTime(), "");
+
+ int year = calendar.get(Calendar.YEAR);
+ int month = calendar.get(Calendar.MONTH);
+ int day = calendar.get(Calendar.DAY_OF_MONTH);
+ int hours = calendar.get(Calendar.HOUR_OF_DAY);
+ int minutes = calendar.get(Calendar.MINUTE);
+ String id = "" + year + month + day + hours + minutes + gReading.getReading();
+
+ // Check for duplicates
+ if (getGlucoseReadingById(Long.parseLong(id)) == null) {
+ realm.beginTransaction();
+ gReading.setId(Long.parseLong(id));
+ realm.copyToRealm(gReading);
+ realm.commitTransaction();
+ }
+ }
+ }
+
+ public boolean editGlucoseReading(long oldId, GlucoseReading reading) {
+ // First delete the old reading
+ deleteGlucoseReading(getGlucoseReadingById(oldId));
+ // then save the new one
+ return addGlucoseReading(reading);
+ }
+
+ public void deleteGlucoseReading(GlucoseReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public GlucoseReading getLastGlucoseReading() {
+ RealmResults results =
+ realm.where(GlucoseReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ return results.get(0);
+ }
+
+ public List getGlucoseReadings() {
+ RealmResults results =
+ realm.where(GlucoseReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ return new ArrayList<>(results);
+ }
+
+ public List getGlucoseReadings(Realm realm) {
+ RealmResults results =
+ realm.where(GlucoseReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ return new ArrayList<>(results);
+ }
+
+ private ArrayList getGlucoseReadings(Date from, Date to) {
+ RealmResults results =
+ realm.where(GlucoseReading.class)
+ .between(RealmField.CREATED.key(), from, to)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ return new ArrayList<>(results);
+ }
+
+ public List getGlucoseReadings(Realm realm, Date from, Date to) {
+ RealmResults results =
+ realm.where(GlucoseReading.class)
+ .between(RealmField.CREATED.key(), from, to)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ return new ArrayList<>(results);
+ }
+
+ public GlucoseReading getGlucoseReadingById(long id) {
+ return realm.where(GlucoseReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public List getGlucoseIdAsList() {
+ List glucoseReading = getGlucoseReadings();
+ List idArray = new ArrayList<>(glucoseReading.size());
+
+ for (GlucoseReading aGlucoseReading : glucoseReading) {
+ long id = aGlucoseReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getGlucoseReadingAsList() {
+ List glucoseReading = getGlucoseReadings();
+ ArrayList readingArray = new ArrayList<>(glucoseReading.size());
+ int i;
+
+ for (i = 0; i < glucoseReading.size(); i++) {
+ GlucoseReading singleReading = glucoseReading.get(i);
+ double reading = singleReading.getReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getGlucoseTypeAsList() {
+ List glucoseReading = getGlucoseReadings();
+ List typeArray = new ArrayList<>(glucoseReading.size());
+
+ for (GlucoseReading aGlucoseReading : glucoseReading) {
+ String reading = aGlucoseReading.getReading_type();
+ typeArray.add(reading);
+ }
+
+ return typeArray;
+ }
+
+ public List getGlucoseNotesAsList() {
+ List glucoseReading = getGlucoseReadings();
+ ArrayList notesArray = new ArrayList<>(glucoseReading.size());
+
+ for (GlucoseReading aGlucoseReading : glucoseReading) {
+ String reading = aGlucoseReading.getNotes();
+ notesArray.add(reading);
+ }
+
+ return notesArray;
+ }
+
+ public List getGlucoseDateTimeAsList() {
+ List glucoseReading = getGlucoseReadings();
+ ArrayList datetimeArray = new ArrayList<>(glucoseReading.size());
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (GlucoseReading singleReading : glucoseReading) {
+ String reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ public List getAverageGlucoseReadingsByWeek() {
+ JodaTimeAndroid.init(mContext);
+
+ DateTime maxDateTime = new DateTime(realm.where(GlucoseReading.class).maximumDate(RealmField.CREATED.key()).getTime());
+ DateTime minDateTime = new DateTime(realm.where(GlucoseReading.class).minimumDate(RealmField.CREATED.key()).getTime());
+
+ DateTime currentDateTime = minDateTime;
+ DateTime newDateTime;
+
+ int weeksNumber = Weeks.weeksBetween(minDateTime, maxDateTime).getWeeks();
+ List averageReadings = new ArrayList<>(weeksNumber);
+
+ // The number of weeks is at least 1 since we do have average for the current week even if incomplete
+ for (int i = 0; i < weeksNumber + 1; i++) {
+ newDateTime = currentDateTime.plusWeeks(1);
+ RealmResults readings = realm.where(GlucoseReading.class)
+ .between(RealmField.CREATED.key(), currentDateTime.toDate(), newDateTime.toDate())
+ .findAll();
+ averageReadings.add(readings.average("reading"));
+ currentDateTime = newDateTime;
+ }
+ return averageReadings;
+ }
+
+ public List getGlucoseDatetimesByWeek() {
+ JodaTimeAndroid.init(mContext);
+
+ DateTime maxDateTime = new DateTime(realm.where(GlucoseReading.class).maximumDate(RealmField.CREATED.key()).getTime());
+ DateTime minDateTime = new DateTime(realm.where(GlucoseReading.class).minimumDate(RealmField.CREATED.key()).getTime());
+
+ DateTime currentDateTime = minDateTime;
+ DateTime newDateTime;
+
+ List finalWeeks = new ArrayList<>();
+
+ // The number of weeks is at least 1 since we do have average for the current week even if incomplete
+ int weeksNumber = Weeks.weeksBetween(minDateTime, maxDateTime).getWeeks() + 1;
+
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ for (int i = 0; i < weeksNumber; i++) {
+ newDateTime = currentDateTime.plusWeeks(1);
+ finalWeeks.add(inputFormat.format(newDateTime.toDate()));
+ currentDateTime = newDateTime;
+ }
+ return finalWeeks;
+ }
+
+ public List getAverageGlucoseReadingsByMonth() {
+ JodaTimeAndroid.init(mContext);
+
+ DateTime maxDateTime = new DateTime(realm.where(GlucoseReading.class).maximumDate(RealmField.CREATED.key()).getTime());
+ DateTime minDateTime = new DateTime(realm.where(GlucoseReading.class).minimumDate(RealmField.CREATED.key()).getTime());
+
+ DateTime currentDateTime = minDateTime;
+ DateTime newDateTime;
+
+ int months = Months.monthsBetween(minDateTime, maxDateTime).getMonths();
+ List averageReadings = new ArrayList<>(months);
+
+ // The number of months is at least 1 since we do have average for the current week even if incomplete
+ int monthsNumber = months + 1;
+
+ for (int i = 0; i < monthsNumber; i++) {
+ newDateTime = currentDateTime.plusMonths(1);
+ RealmResults readings = realm.where(GlucoseReading.class)
+ .between(RealmField.CREATED.key(), currentDateTime.toDate(), newDateTime.toDate())
+ .findAll();
+ averageReadings.add(readings.average("reading"));
+ currentDateTime = newDateTime;
+ }
+ return averageReadings;
+ }
+
+ public List getGlucoseDatetimesByMonth() {
+ JodaTimeAndroid.init(mContext);
+
+ DateTime maxDateTime = new DateTime(realm.where(GlucoseReading.class).maximumDate(RealmField.CREATED.key()).getTime());
+ DateTime minDateTime = new DateTime(realm.where(GlucoseReading.class).minimumDate(RealmField.CREATED.key()).getTime());
+
+ DateTime currentDateTime = minDateTime;
+ DateTime newDateTime;
+
+ ArrayList finalMonths = new ArrayList<>();
+
+ // The number of months is at least 1 because current month is incomplete
+ int monthsNumber = Months.monthsBetween(minDateTime, maxDateTime).getMonths() + 1;
+
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ for (int i = 0; i < monthsNumber; i++) {
+ newDateTime = currentDateTime.plusMonths(1);
+ finalMonths.add(inputFormat.format(newDateTime.toDate()));
+ currentDateTime = newDateTime;
+ }
+ return finalMonths;
+ }
+
+ public List getLastMonthGlucoseReadings() {
+ JodaTimeAndroid.init(mContext);
+
+ DateTime todayDateTime = DateTime.now();
+ DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
+
+ return getGlucoseReadings(minDateTime.toDate(), todayDateTime.toDate());
+ }
+
+ public void addHB1ACReading(HB1ACReading reading) {
+ realm.beginTransaction();
+ reading.setId(getNextKey("hb1ac"));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ }
+
+ public void deleteHB1ACReading(HB1ACReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public HB1ACReading getHB1ACReadingById(long id) {
+ return realm.where(HB1ACReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void editHB1ACReading(long oldId, HB1ACReading reading) {
+ // First delete the old reading
+ deleteHB1ACReading(getHB1ACReadingById(oldId));
+ // then save the new one
+ addHB1ACReading(reading);
+ }
+
+ public RealmResults getrHB1ACRawReadings() {
+ return realm.where(HB1ACReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ }
+
+ public List getHB1ACReadings() {
+ RealmResults results =
+ realm.where(HB1ACReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+
+ return new ArrayList<>(results);
+ }
+
+ public List getHB1ACIdAsArray() {
+ List readings = getHB1ACReadings();
+ ArrayList idArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ long id;
+ HB1ACReading singleReading = readings.get(i);
+ id = singleReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getHB1ACReadingAsArray() {
+ List readings = getHB1ACReadings();
+ ArrayList readingArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ double reading;
+ HB1ACReading singleReading = readings.get(i);
+ reading = singleReading.getReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getHB1ACDateTimeAsArray() {
+ List readings = getHB1ACReadings();
+ ArrayList datetimeArray = new ArrayList<>();
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ HB1ACReading singleReading = readings.get(i);
+ reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ public RealmResults getRawKetoneReadings() {
+ return realm.where(KetoneReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+ }
+
+ public void addKetoneReading(KetoneReading reading) {
+ realm.beginTransaction();
+ reading.setId(getNextKey("ketone"));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ }
+
+ public void editKetoneReading(long oldId, KetoneReading reading) {
+ // First delete the old reading
+ deleteKetoneReading(getKetoneReadingById(oldId));
+ // then save the new one
+ addKetoneReading(reading);
+ }
+
+ public KetoneReading getKetoneReadingById(long id) {
+ return realm.where(KetoneReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void deleteKetoneReading(KetoneReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public List getKetoneReadings() {
+ RealmResults results =
+ realm.where(KetoneReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+
+ return new ArrayList<>(results);
+ }
+
+ public List getKetoneIdAsArray() {
+ List readings = getKetoneReadings();
+ ArrayList idArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ long id;
+ KetoneReading singleReading = readings.get(i);
+ id = singleReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getKetoneReadingAsArray() {
+ List readings = getKetoneReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ double reading;
+ KetoneReading singleReading = readings.get(i);
+ reading = singleReading.getReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getKetoneDateTimeAsArray() {
+ List readings = getKetoneReadings();
+ ArrayList datetimeArray = new ArrayList<>();
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ KetoneReading singleReading = readings.get(i);
+ reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ public void addPressureReading(PressureReading reading) {
+ realm.beginTransaction();
+ reading.setId(getNextKey("pressure"));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ }
+
+ public PressureReading getPressureReading(long id) {
+ return realm.where(PressureReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void deletePressureReading(PressureReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public void editPressureReading(long oldId, PressureReading reading) {
+ // First delete the old reading
+ deletePressureReading(getPressureReading(oldId));
+ // then save the new one
+ addPressureReading(reading);
+ }
+
+ public List getPressureReadings() {
+ RealmResults results =
+ realm.where(PressureReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+
+ return new ArrayList<>(results);
+ }
+
+ public List getPressureIdAsArray() {
+ List readings = getPressureReadings();
+ ArrayList idArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ long id;
+ PressureReading singleReading = readings.get(i);
+ id = singleReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getMinPressureReadingAsArray() {
+ List readings = getPressureReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ PressureReading singleReading = readings.get(i);
+ double reading = singleReading.getMinReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getMaxPressureReadingAsArray() {
+ List readings = getPressureReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ PressureReading singleReading = readings.get(i);
+ double reading = singleReading.getMaxReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getPressureDateTimeAsArray() {
+ List readings = getPressureReadings();
+ ArrayList datetimeArray = new ArrayList<>();
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ PressureReading singleReading = readings.get(i);
+ reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ public void addWeightReading(WeightReading reading) {
+ realm.beginTransaction();
+ reading.setId(getNextKey("weight"));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ }
+
+ public void editWeightReading(long oldId, WeightReading reading) {
+ // First delete the old reading
+ deleteWeightReading(getWeightReadingById(oldId));
+ // then save the new one
+ addWeightReading(reading);
+ }
+
+ public WeightReading getWeightReadingById(long id) {
+ return realm.where(WeightReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void deleteWeightReading(WeightReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public List getWeightReadings() {
+ RealmResults results =
+ realm.where(WeightReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+
+ return new ArrayList<>(results);
+ }
+
+ public List getWeightIdAsArray() {
+ List readings = getWeightReadings();
+ ArrayList idArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ long id;
+ WeightReading singleReading = readings.get(i);
+ id = singleReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getWeightReadingAsArray() {
+ List readings = getWeightReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ WeightReading singleReading = readings.get(i);
+ double reading = singleReading.getReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getWeightReadingDateTimeAsArray() {
+ List readings = getWeightReadings();
+ ArrayList datetimeArray = new ArrayList<>();
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ WeightReading singleReading = readings.get(i);
+ reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ public void addCholesterolReading(CholesterolReading reading) {
+ realm.beginTransaction();
+ reading.setId(getNextKey("cholesterol"));
+ realm.copyToRealm(reading);
+ realm.commitTransaction();
+ }
+
+ public void editCholesterolReading(long oldId, CholesterolReading reading) {
+ // First delete the old reading
+ deleteCholesterolReading(getCholesterolReading(oldId));
+ // then save the new one
+ addCholesterolReading(reading);
+ }
+
+ public CholesterolReading getCholesterolReading(long id) {
+ return realm.where(CholesterolReading.class)
+ .equalTo("id", id)
+ .findFirst();
+ }
+
+ public void deleteCholesterolReading(CholesterolReading reading) {
+ realm.beginTransaction();
+ reading.deleteFromRealm();
+ realm.commitTransaction();
+ }
+
+ public List getCholesterolReadings() {
+ RealmResults results =
+ realm.where(CholesterolReading.class)
+ .sort(RealmField.CREATED.key(), Sort.DESCENDING)
+ .findAll();
+
+ return new ArrayList<>(results);
+ }
+
+ public List getCholesterolIdAsArray() {
+ List readings = getCholesterolReadings();
+ ArrayList idArray = new ArrayList<>();
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ long id;
+ CholesterolReading singleReading = readings.get(i);
+ id = singleReading.getId();
+ idArray.add(id);
+ }
+
+ return idArray;
+ }
+
+ public List getHDLCholesterolReadingAsArray() {
+ List readings = getCholesterolReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ CholesterolReading singleReading = readings.get(i);
+ double reading = singleReading.getHDLReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getLDLCholesterolReadingAsArray() {
+ List readings = getCholesterolReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ CholesterolReading singleReading = readings.get(i);
+ double reading = singleReading.getLDLReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getTotalCholesterolReadingAsArray() {
+ List readings = getCholesterolReadings();
+ ArrayList readingArray = new ArrayList<>(readings.size());
+ int i;
+
+ for (i = 0; i < readings.size(); i++) {
+ CholesterolReading singleReading = readings.get(i);
+ double reading = singleReading.getTotalReading();
+ readingArray.add(reading);
+ }
+
+ return readingArray;
+ }
+
+ public List getCholesterolDateTimeAsArray() {
+ List readings = getCholesterolReadings();
+ ArrayList datetimeArray = new ArrayList<>();
+ int i;
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+ for (i = 0; i < readings.size(); i++) {
+ String reading;
+ CholesterolReading singleReading = readings.get(i);
+ reading = inputFormat.format(singleReading.getCreated());
+ datetimeArray.add(reading);
+ }
+
+ return datetimeArray;
+ }
+
+ private long getNextKey(String where) {
+ Number maxId = null;
+ switch (where) {
+ case "glucose":
+ maxId = realm.where(GlucoseReading.class)
+ .max("id");
+ break;
+ case "weight":
+ maxId = realm.where(WeightReading.class)
+ .max("id");
+ break;
+ case "hb1ac":
+ maxId = realm.where(HB1ACReading.class)
+ .max("id");
+ break;
+ case "pressure":
+ maxId = realm.where(PressureReading.class)
+ .max("id");
+ break;
+ case "ketone":
+ maxId = realm.where(KetoneReading.class)
+ .max("id");
+ break;
+ case "cholesterol":
+ maxId = realm.where(CholesterolReading.class)
+ .max("id");
+ break;
+ }
+ if (maxId == null) {
+ return 0;
+ } else {
+ return Long.parseLong(maxId.toString()) + 1;
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/GlucoseReading.java b/app/src/main/java/org/glucosio/android/db/GlucoseReading.java
new file mode 100644
index 0000000..f1d556d
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/GlucoseReading.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class GlucoseReading extends RealmObject {
+
+ @PrimaryKey
+ private long id;
+
+ private double reading;
+ private String reading_type;
+ private String notes;
+ private int user_id;
+ private Date created;
+
+ public GlucoseReading() {
+ }
+
+ public GlucoseReading(double reading, String reading_type, Date created, String notes) {
+ this.reading = reading;
+ this.reading_type = reading_type;
+ this.created = created;
+ this.notes = notes;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public double getReading() {
+ return reading;
+ }
+
+ public void setReading(double reading) {
+ this.reading = reading;
+ }
+
+ public String getReading_type() {
+ return reading_type;
+ }
+
+ public void setReading_type(String reading_type) {
+ this.reading_type = reading_type;
+ }
+
+ public String getNotes() {
+ return notes;
+ }
+
+ public void setNotes(String notes) {
+ this.notes = notes;
+ }
+
+ public int getUser_id() {
+ return user_id;
+ }
+
+ public void setUser_id(int user_id) {
+ this.user_id = user_id;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/HB1ACReading.java b/app/src/main/java/org/glucosio/android/db/HB1ACReading.java
new file mode 100644
index 0000000..643707a
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/HB1ACReading.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class HB1ACReading extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private double reading;
+ private Date created;
+
+ public HB1ACReading() {
+ }
+
+ public HB1ACReading(double reading, Date created) {
+ // %
+ this.reading = reading;
+ this.created = created;
+ }
+
+ public double getReading() {
+ return reading;
+ }
+
+ public void setReading(double reading) {
+ this.reading = reading;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/KetoneReading.java b/app/src/main/java/org/glucosio/android/db/KetoneReading.java
new file mode 100644
index 0000000..3ea6bcb
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/KetoneReading.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class KetoneReading extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private double reading;
+ private Date created;
+
+ public KetoneReading() {
+ }
+
+ public KetoneReading(double reading, Date created) {
+ this.reading = reading;
+ this.created = created;
+ }
+
+ public double getReading() {
+ return reading;
+ }
+
+ public void setReading(double reading) {
+ this.reading = reading;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/Migration.java b/app/src/main/java/org/glucosio/android/db/Migration.java
new file mode 100644
index 0000000..84a287b
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/Migration.java
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+
+import java.util.Date;
+
+import io.realm.DynamicRealm;
+import io.realm.DynamicRealmObject;
+import io.realm.FieldAttribute;
+import io.realm.RealmMigration;
+import io.realm.RealmObjectSchema;
+import io.realm.RealmSchema;
+
+/************************************************
+ Current Database Schema
+
+ class User
+ @PrimaryKey int id;
+ String name;
+ String preferred_language;
+ String country;
+ int age;
+ String gender;
+ int d_type;
+ String preferred_unit;
+ String preferred_unit_a1c;
+ String preferred_unit_weight;
+ String preferred_range;
+ double custom_range_min;
+ double custom_range_max;
+
+ class Reminder
+ @PrimaryKey long id;
+ Date alarmTime;
+ boolean oneTime;
+ String metric;
+
+ class CholesterolReading
+ @PrimaryKey long id;
+ double totalReading;
+ double LDLReading;
+ double HDLReading;
+ Date created;
+
+ class GlucoseReading
+ @PrimaryKey long id;
+ double reading;
+ String reading_type;
+ String notes;
+ int user_id;
+ Date created;
+
+ class KetoneReading
+ @PrimaryKey long id;
+ double reading;
+ Date created;
+
+ class PressureReading
+ @PrimaryKey long id;
+ double minReading;
+ double maxReading;
+ Date created;
+
+ class WeightReading
+ @PrimaryKey long id;
+ double reading;
+ Date created;
+
+ class HB1ACReading
+ @PrimaryKey long id;
+ double reading;
+ Date created;
+ ************************************************/
+
+class Migration implements RealmMigration {
+
+ @Override
+ public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
+ RealmSchema schema = realm.getSchema();
+
+ if (oldVersion == 0) {
+ createInitialSchema(schema);
+ oldVersion++;
+ }
+
+ if (oldVersion == 1) {
+ // Change HB1AC reading from int to double
+ changeHB1ACReadingsToDouble(schema);
+
+ oldVersion++;
+ }
+
+ if (oldVersion == 2) {
+ // Add 2 new fields in User:
+ // String preferred_unit_a1c
+ // String preferred_unit_weight
+ // and populate them with default values (% and kilograms)
+ addWeightAndA1CUnitsToUser(schema);
+
+ oldVersion++;
+ }
+
+ if (oldVersion == 3) {
+ // Add Reminders
+ addReminders(schema);
+
+ oldVersion++;
+ }
+
+ if (oldVersion == 4) {
+ migrateAllReadingsToDouble(schema);
+
+ oldVersion++;
+ }
+ }
+
+ private void migrateAllReadingsToDouble(RealmSchema schema) {
+ // Change Weight reading from int to double
+ RealmObjectSchema objectSchema = schema.get(RObject.WEIGHT.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.READING.key());
+
+ // Change Pressure max and min readings from int to double
+ objectSchema = schema.get(RObject.PRESSURE.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.MIN_READING.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.MAX_READING.key());
+
+ // Change Glucose reading from int to double
+ objectSchema = schema.get(RObject.GLUCOSE.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.READING.key());
+
+ // Change Cholesterol total, ldl and hdl readings from int to double
+ objectSchema = schema.get(RObject.CHOLESTEROL.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.TOTAL_READING.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.LDL_READING.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.HDL_READING.key());
+
+ // Change User custom range min and max from int to double
+ objectSchema = schema.get(RObject.USER.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.CUSTOM_RANGE_MIN.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.CUSTOM_RANGE_MAX.key());
+ }
+
+ private void addReminders(RealmSchema schema) {
+ schema.create(RObject.REMINDER.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.METRIC.key(), String.class)
+ .addField(RealmField.ALARM_TIME.key(), Date.class)
+ .addField(RealmField.ACTIVE.key(), Boolean.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.ONE_TIME.key(), Boolean.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.LABEL.key(), String.class);
+ }
+
+ private void addWeightAndA1CUnitsToUser(RealmSchema schema) {
+ schema.get(RObject.USER.key())
+ .addField(RealmField.PREFERRED_UNIT_A1C.key(), String.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.PREFERRED_UNIT_WEIGHT.key(), String.class, FieldAttribute.REQUIRED)
+ .transform(new RealmObjectSchema.Function() {
+ @Override
+ public void apply(DynamicRealmObject obj) {
+ obj.set(RealmField.PREFERRED_UNIT_A1C.key(), "percentage");
+ obj.set(RealmField.PREFERRED_UNIT_WEIGHT.key(), "kilograms");
+ }
+ });
+ }
+
+ private void changeHB1ACReadingsToDouble(RealmSchema schema) {
+ RealmObjectSchema objectSchema = schema.get(RObject.HB_1_AC.key());
+ safeMigrationIntToDouble(objectSchema, RealmField.READING.key());
+ }
+
+ private void createInitialSchema(RealmSchema schema) {
+ schema.create(RObject.WEIGHT.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.CREATED.key(), Date.class)
+ .addField(RealmField.READING.key(), Integer.class, FieldAttribute.REQUIRED);
+
+ schema.create(RObject.PRESSURE.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.CREATED.key(), Date.class)
+ .addField(RealmField.MIN_READING.key(), Integer.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.MAX_READING.key(), Integer.class, FieldAttribute.REQUIRED);
+
+ schema.create(RObject.KETONE.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.CREATED.key(), Date.class)
+ .addField(RealmField.READING.key(), Double.class, FieldAttribute.REQUIRED);
+
+ schema.create(RObject.HB_1_AC.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.CREATED.key(), Date.class)
+ .addField(RealmField.READING.key(), Integer.class, FieldAttribute.REQUIRED);
+
+ schema.create(RObject.CHOLESTEROL.key())
+ .addField(RealmField.ID.key(), Long.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
+ .addField(RealmField.CREATED.key(), Date.class)
+ .addField(RealmField.TOTAL_READING.key(), Integer.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.LDL_READING.key(), Integer.class, FieldAttribute.REQUIRED)
+ .addField(RealmField.HDL_READING.key(), Integer.class, FieldAttribute.REQUIRED);
+ }
+
+ private void safeMigrationIntToDouble(@Nullable RealmObjectSchema objectSchema, @NonNull String columnName) {
+ if (objectSchema != null) {
+ migrateIntColumnToDouble(objectSchema, columnName);
+ }
+ }
+
+ private void migrateIntColumnToDouble(@NonNull RealmObjectSchema objectSchema, @NonNull final String columnName) {
+ final String tempColumnName = columnName + "_tmp";
+ objectSchema
+ .addField(tempColumnName, Double.class, FieldAttribute.REQUIRED)
+ .transform(new RealmObjectSchema.Function() {
+ @Override
+ public void apply(@NonNull DynamicRealmObject obj) {
+ int oldType = obj.getInt(columnName);
+ obj.setDouble(tempColumnName, oldType);
+ }
+ })
+ .removeField(columnName)
+ .renameField(tempColumnName, columnName);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/PressureReading.java b/app/src/main/java/org/glucosio/android/db/PressureReading.java
new file mode 100644
index 0000000..fbb9f69
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/PressureReading.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class PressureReading extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private double minReading;
+ private double maxReading;
+ private Date created;
+
+ public PressureReading() {
+ }
+
+ public PressureReading(double minReading, double maxReading, Date created) {
+ // mm/Hg
+ this.minReading = minReading;
+ this.maxReading = maxReading;
+ this.created = created;
+ }
+
+ public double getMinReading() {
+ return minReading;
+ }
+
+ public void setMinReading(double minReading) {
+ this.minReading = minReading;
+ }
+
+ public double getMaxReading() {
+ return maxReading;
+ }
+
+ public void setMaxReading(double maxReading) {
+ this.maxReading = maxReading;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/RObject.java b/app/src/main/java/org/glucosio/android/db/RObject.java
new file mode 100644
index 0000000..c742852
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/RObject.java
@@ -0,0 +1,25 @@
+package org.glucosio.android.db;
+
+/**
+ * I can not leave it as Object and I can not name it as RealmObject
+ */
+public enum RObject {
+ GLUCOSE("GlucoseReading"),
+ HB_1_AC("HB_1_ACReading"),
+ WEIGHT("WeightReading"),
+ PRESSURE("PressureReading"),
+ KETONE("KetoneReading"),
+ CHOLESTEROL("CholesterolReading"),
+ USER("User"),
+ REMINDER("Reminder");
+
+ private String key;
+
+ RObject(String key) {
+ this.key = key;
+ }
+
+ String key() {
+ return key;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/RealmField.java b/app/src/main/java/org/glucosio/android/db/RealmField.java
new file mode 100644
index 0000000..ae87e0f
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/RealmField.java
@@ -0,0 +1,31 @@
+package org.glucosio.android.db;
+
+public enum RealmField {
+ ID("id"),
+ CREATED("created"),
+ READING("reading"),
+ MIN_READING("minReading"),
+ MAX_READING("maxReading"),
+ TOTAL_READING("totalReading"),
+ LDL_READING("LDLReading"),
+ HDL_READING("HDLReading"),
+ CUSTOM_RANGE_MIN("custom_range_min"),
+ CUSTOM_RANGE_MAX("custom_range_max"),
+ PREFERRED_UNIT_A1C("preferred_unit_a1c"),
+ PREFERRED_UNIT_WEIGHT("preferred_unit_weight"),
+ METRIC("metric"),
+ ALARM_TIME("alarmTime"),
+ ACTIVE("active"),
+ ONE_TIME("oneTime"),
+ LABEL("label");
+
+ private String key;
+
+ RealmField(String key) {
+ this.key = key;
+ }
+
+ String key() {
+ return key;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/Reminder.java b/app/src/main/java/org/glucosio/android/db/Reminder.java
new file mode 100644
index 0000000..ca91cab
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/Reminder.java
@@ -0,0 +1,77 @@
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class Reminder extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private Date alarmTime;
+ private boolean oneTime;
+ private boolean active;
+ private String label;
+ private String metric;
+
+ public Reminder() {
+ }
+
+ public Reminder(long id, Date alarmTime, String label, String metric, boolean oneTime, boolean active) {
+ this.id = id;
+ this.label = label;
+ this.alarmTime = alarmTime;
+ this.metric = metric;
+ this.oneTime = oneTime;
+ this.active = active;
+ }
+
+ public Date getAlarmTime() {
+ return alarmTime;
+ }
+
+ public void setAlarmTime(Date alarmTime) {
+ this.alarmTime = alarmTime;
+ }
+
+ public boolean isOneTime() {
+ return oneTime;
+ }
+
+ public void setOneTime(boolean oneTime) {
+ this.oneTime = oneTime;
+ }
+
+ public String getMetric() {
+ return metric;
+ }
+
+ public void setMetric(String metric) {
+ this.metric = metric;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public boolean isActive() {
+ return active;
+ }
+
+ public void setActive(boolean active) {
+ this.active = active;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public void setLabel(String label) {
+ this.label = label;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/User.java b/app/src/main/java/org/glucosio/android/db/User.java
new file mode 100644
index 0000000..fd7f185
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/User.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+import io.realm.annotations.Required;
+
+public class User extends RealmObject {
+
+ @PrimaryKey
+ private int id;
+ private String name;
+ private String preferred_language;
+ private String country;
+ private int age;
+ private String gender;
+ private int d_type;
+ private String preferred_unit;
+ @Required
+ private String preferred_unit_a1c;
+ @Required
+ private String preferred_unit_weight;
+ private String preferred_range;
+ private double custom_range_min;
+ private double custom_range_max;
+
+ public User() {
+
+ }
+
+ public User(User copy) {
+ id = copy.id;
+ name = copy.name;
+ preferred_language = copy.preferred_language;
+ country = copy.country;
+ age = copy.age;
+ gender = copy.gender;
+ d_type = copy.d_type;
+ preferred_unit = copy.preferred_unit;
+ preferred_unit_a1c = copy.preferred_unit_a1c;
+ preferred_unit_weight = copy.preferred_unit_weight;
+ preferred_range = copy.preferred_range;
+ custom_range_max = copy.custom_range_max;
+ custom_range_min = copy.custom_range_min;
+ }
+
+ User(int id, String name, String preferred_language, String country, int age, String gender, int dType,
+ String pUnit, String a1cUnit, String weightUnit, String pRange, double minRange, double maxRange) {
+ this.id = id;
+ this.name = name;
+ this.preferred_language = preferred_language;
+ this.country = country;
+ this.age = age;
+ this.gender = gender;
+ this.d_type = dType;
+ this.preferred_unit = pUnit;
+ this.preferred_unit_a1c = a1cUnit;
+ this.preferred_unit_weight = weightUnit;
+ this.preferred_range = pRange;
+ this.custom_range_max = maxRange;
+ this.custom_range_min = minRange;
+ }
+
+ public int getD_type() {
+ return this.d_type;
+ }
+
+ public void setD_type(int dType) {
+ this.d_type = dType;
+ }
+
+ public String getPreferred_unit() {
+ return this.preferred_unit;
+ }
+
+ public void setPreferred_unit(String pUnit) {
+ this.preferred_unit = pUnit;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getCountry() {
+ return this.country;
+ }
+
+ public void setCountry(String country) {
+ this.country = country;
+ }
+
+ public String getPreferred_language() {
+ return this.preferred_language;
+ }
+
+ public void setPreferred_language(String preferred_language) {
+ this.preferred_language = preferred_language;
+ }
+
+ public int getAge() {
+ return this.age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getGender() {
+ return this.gender;
+ }
+
+ public void setGender(String gender) {
+ this.gender = gender;
+ }
+
+ public String getPreferred_range() {
+ return preferred_range;
+ }
+
+ public void setPreferred_range(String preferred_range) {
+ this.preferred_range = preferred_range;
+ }
+
+ public double getCustom_range_min() {
+ return custom_range_min;
+ }
+
+ public void setCustom_range_min(double custom_range_min) {
+ this.custom_range_min = custom_range_min;
+ }
+
+ public double getCustom_range_max() {
+ return custom_range_max;
+ }
+
+ public void setCustom_range_max(double custom_range_max) {
+ this.custom_range_max = custom_range_max;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getPreferred_unit_a1c() {
+ return preferred_unit_a1c;
+ }
+
+ public void setPreferred_unit_a1c(String preferred_unit_a1c) {
+ this.preferred_unit_a1c = preferred_unit_a1c;
+ }
+
+ public String getPreferred_unit_weight() {
+ return preferred_unit_weight;
+ }
+
+ public void setPreferred_unit_weight(String preferred_unit_weight) {
+ this.preferred_unit_weight = preferred_unit_weight;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/UserBuilder.java b/app/src/main/java/org/glucosio/android/db/UserBuilder.java
new file mode 100644
index 0000000..697a64e
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/UserBuilder.java
@@ -0,0 +1,87 @@
+package org.glucosio.android.db;
+
+public class UserBuilder {
+ private int id;
+ private String name;
+ private String preferredLanguage;
+ private String country;
+ private int age;
+ private String gender;
+ private int diabetesType;
+ private String preferredUnit;
+ private String a1cUnit;
+ private String preferredWeightUnit;
+ private String pRange;
+ private double minRange;
+ private double maxRange;
+
+ public UserBuilder setId(int id) {
+ this.id = id;
+ return this;
+ }
+
+ public UserBuilder setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public UserBuilder setPreferredLanguage(String language) {
+ this.preferredLanguage = language;
+ return this;
+ }
+
+ public UserBuilder setCountry(String country) {
+ this.country = country;
+ return this;
+ }
+
+ public UserBuilder setAge(int age) {
+ this.age = age;
+ return this;
+ }
+
+ public UserBuilder setGender(String gender) {
+ this.gender = gender;
+ return this;
+ }
+
+ public UserBuilder setDiabetesType(int dType) {
+ this.diabetesType = dType;
+ return this;
+ }
+
+ public UserBuilder setPreferredUnit(String unit) {
+ this.preferredUnit = unit;
+ return this;
+ }
+
+ public UserBuilder setPreferredA1CUnit(String a1cUnit) {
+ this.a1cUnit = a1cUnit;
+ return this;
+ }
+
+ public UserBuilder setPreferredWeightUnit(String weightUnit) {
+ this.preferredWeightUnit = weightUnit;
+ return this;
+ }
+
+ public UserBuilder setPreferredRange(String pRange) {
+ this.pRange = pRange;
+ return this;
+ }
+
+ public UserBuilder setMinRange(double minRange) {
+ this.minRange = minRange;
+ return this;
+ }
+
+ public UserBuilder setMaxRange(double maxRange) {
+ this.maxRange = maxRange;
+ return this;
+ }
+
+ public User createUser() {
+ return new User(id, name, preferredLanguage, country, age, gender, diabetesType, preferredUnit, a1cUnit,
+ preferredWeightUnit, pRange, minRange, maxRange);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/db/WeightReading.java b/app/src/main/java/org/glucosio/android/db/WeightReading.java
new file mode 100644
index 0000000..9af509c
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/db/WeightReading.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.db;
+
+import java.util.Date;
+
+import io.realm.RealmObject;
+import io.realm.annotations.PrimaryKey;
+
+public class WeightReading extends RealmObject {
+ @PrimaryKey
+ private long id;
+
+ private double reading;
+ private Date created;
+
+ public WeightReading() {
+ }
+
+ public WeightReading(double reading, Date created) {
+ this.reading = reading;
+ this.created = created;
+ }
+
+ public double getReading() {
+ return reading;
+ }
+
+ public void setReading(double reading) {
+ this.reading = reading;
+ }
+
+ public Date getCreated() {
+ return created;
+ }
+
+ public void setCreated(Date created) {
+ this.created = created;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/fragment/AssistantFragment.java b/app/src/main/java/org/glucosio/android/fragment/AssistantFragment.java
new file mode 100644
index 0000000..3631d63
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/fragment/AssistantFragment.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.fragment;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.helper.ItemTouchHelper;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.animation.Animation;
+import android.view.animation.TranslateAnimation;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import org.glucosio.android.R;
+import org.glucosio.android.activity.A1cCalculatorActivity;
+import org.glucosio.android.activity.AddGlucoseActivity;
+import org.glucosio.android.activity.MainActivity;
+import org.glucosio.android.adapter.AssistantAdapter;
+import org.glucosio.android.object.ActionTip;
+import org.glucosio.android.presenter.AssistantPresenter;
+
+import java.util.ArrayList;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import butterknife.OnClick;
+import butterknife.Unbinder;
+
+public class AssistantFragment extends Fragment {
+
+ @BindView(R.id.fragment_tips_recyclerview)
+ RecyclerView tipsRecycler;
+ @BindView(R.id.fragment_assistant_archived)
+ LinearLayout archivedButton;
+ @BindView(R.id.fragment_assistant_archived_dismiss)
+ LinearLayout archivedDismissButton;
+ private SharedPreferences sharedPref;
+ private AssistantAdapter adapter;
+ private ArrayList actionTips;
+ private String[] actionTipTitles;
+ private String[] actionTipDescriptions;
+ private String[] actionTipActions;
+
+ private Unbinder unbinder;
+
+ public static AssistantFragment newInstance() {
+ return new AssistantFragment();
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ AssistantPresenter presenter = new AssistantPresenter(this);
+ actionTips = new ArrayList<>();
+
+ actionTipTitles = getResources().getStringArray(R.array.assistant_titles);
+ actionTipDescriptions = getResources().getStringArray(R.array.assistant_descriptions);
+ actionTipActions = getResources().getStringArray(R.array.assistant_actions);
+ populateWithNewTips();
+
+ View view = inflater.inflate(R.layout.fragment_assistant, container, false);
+ unbinder = ButterKnife.bind(this, view);
+
+ adapter = new AssistantAdapter(presenter, getActivity().getApplicationContext().getResources(), actionTips);
+
+ LinearLayoutManager llm = new LinearLayoutManager(getActivity());
+ llm.setOrientation(LinearLayoutManager.VERTICAL);
+ tipsRecycler.setLayoutManager(llm);
+ tipsRecycler.setAdapter(adapter);
+ tipsRecycler.setHasFixedSize(false);
+
+ initSwipeToRemoveTouchHelper();
+
+ // If there aren't dismissed tips, don't show archive button
+ if (actionTipTitles.length == adapter.getItemCount()) {
+ archivedButton.setVisibility(View.GONE);
+ }
+
+ return view;
+ }
+
+ @Override
+ public void onDestroyView() {
+ unbinder.unbind();
+
+ super.onDestroyView();
+ }
+
+ // Swipe to remove functionality
+ private void initSwipeToRemoveTouchHelper() {
+ ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
+ @Override
+ public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
+ return false;
+ }
+
+ @Override
+ public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
+ if (archivedDismissButton.getVisibility() == View.VISIBLE) {
+ // If we're in archive, restore tips
+ TextView title = viewHolder.itemView.findViewById(R.id.fragment_assistant_item_title);
+ removePreference(title.getText().toString());
+
+ int position = viewHolder.getAdapterPosition();
+ actionTips.remove(position);
+ adapter.notifyDataSetChanged();
+ } else {
+ // Else archive them
+ TextView title = viewHolder.itemView.findViewById(R.id.fragment_assistant_item_title);
+ addPreference(title.getText().toString());
+
+ int position = viewHolder.getAdapterPosition();
+ actionTips.remove(position);
+ adapter.notifyDataSetChanged();
+
+ ((MainActivity) getActivity()).reloadFragmentAdapter();
+ }
+ }
+ };
+
+ final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
+
+ itemTouchHelper.attachToRecyclerView(tipsRecycler);
+ }
+
+ @OnClick(R.id.fragment_assistant_archived_dismiss)
+ void archivedDismissButtonClick() {
+ populateWithNewTips();
+ adapter.notifyDataSetChanged();
+ tipsRecycler.swapAdapter(adapter, false);
+ archivedDismissButton.setVisibility(View.GONE);
+ archivedButton.setVisibility(View.VISIBLE);
+
+ ((MainActivity) getActivity()).reloadFragmentAdapter();
+ }
+
+ @OnClick(R.id.fragment_assistant_archived)
+ void archivedButtonClicked() {
+ populateWithArchivedTips();
+ adapter.notifyDataSetChanged();
+ tipsRecycler.swapAdapter(adapter, false);
+ archivedDismissButton.setVisibility(View.VISIBLE);
+ final Animation slide = new TranslateAnimation(0, 0, 0, 200);
+ slide.setDuration(500);
+
+ archivedButton.startAnimation(slide);
+ archivedButton.setVisibility(View.GONE);
+ }
+
+ private void populateWithNewTips() {
+ actionTips.clear();
+ for (int i = 0; i < actionTipTitles.length; i++) {
+ String actionTipTitle = actionTipTitles[i];
+ String actionTipDescription = actionTipDescriptions[i];
+ String actionTipAction = actionTipActions[i];
+
+ ActionTip actionTip = new ActionTip();
+ actionTip.setTipTitle(actionTipTitle);
+ actionTip.setTipDescription(actionTipDescription);
+ actionTip.setTipAction(actionTipAction);
+
+ Boolean value = sharedPref.getBoolean(actionTipTitle, false);
+ if (!value) {
+ actionTips.add(actionTip);
+ }
+ }
+ }
+
+ private void populateWithArchivedTips() {
+ actionTips.clear();
+ for (int i = 0; i < actionTipTitles.length; i++) {
+ String actionTipTitle = actionTipTitles[i];
+ String actionTipDescription = actionTipDescriptions[i];
+ String actionTipAction = actionTipActions[i];
+
+ ActionTip actionTip = new ActionTip();
+ actionTip.setTipTitle(actionTipTitle);
+ actionTip.setTipDescription(actionTipDescription);
+ actionTip.setTipAction(actionTipAction);
+
+ Boolean value = sharedPref.getBoolean(actionTipTitle, false);
+ if (value) {
+ actionTips.add(actionTip);
+ }
+ }
+ }
+
+ private void addPreference(String key) {
+ sharedPref.edit().putBoolean(key, true).apply();
+ }
+
+ private void removePreference(String key) {
+ sharedPref.edit().putBoolean(key, false).apply();
+ }
+
+ public void addReading() {
+ Intent intent = new Intent(getActivity(), AddGlucoseActivity.class);
+ startActivity(intent);
+ getActivity().finish();
+ }
+
+ public void startExportActivity() {
+ ((MainActivity) getActivity()).showExportCsvDialog();
+ }
+
+ public void startA1CCalculatorActivity() {
+ Intent intent = new Intent(getActivity(), A1cCalculatorActivity.class);
+ startActivity(intent);
+ }
+
+ public void openSupportDialog() {
+ ((MainActivity) getActivity()).openSupportDialog();
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/glucosio/android/fragment/HistoryFragment.java b/app/src/main/java/org/glucosio/android/fragment/HistoryFragment.java
new file mode 100644
index 0000000..3c27334
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/fragment/HistoryFragment.java
@@ -0,0 +1,286 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.fragment;
+
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.design.widget.BottomSheetDialog;
+import android.support.design.widget.Snackbar;
+import android.support.v4.app.Fragment;
+import android.support.v4.content.ContextCompat;
+import android.support.v7.widget.CardView;
+import android.support.v7.widget.DefaultItemAnimator;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.LinearLayout;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+import org.glucosio.android.R;
+import org.glucosio.android.activity.AddA1CActivity;
+import org.glucosio.android.activity.AddCholesterolActivity;
+import org.glucosio.android.activity.AddGlucoseActivity;
+import org.glucosio.android.activity.AddKetoneActivity;
+import org.glucosio.android.activity.AddPressureActivity;
+import org.glucosio.android.activity.AddWeightActivity;
+import org.glucosio.android.activity.MainActivity;
+import org.glucosio.android.adapter.HistoryAdapter;
+import org.glucosio.android.listener.ItemClickSupport;
+import org.glucosio.android.presenter.HistoryPresenter;
+import org.glucosio.android.tools.FormatDateTime;
+
+public class HistoryFragment extends Fragment {
+
+ private static final String INTENT_EXTRA_PAGER = "pager";
+ private static final String INTENT_EXTRA_EDITING_ID = "edit_id";
+ private static final String INTENT_EXTRA_EDITING = "editing";
+ private static final String INTENT_EXTRA_DROPDOWN = "history_dropdown";
+
+ private RecyclerView mRecyclerView;
+ private LinearLayoutManager mLayoutManager;
+ private RecyclerView.Adapter mAdapter;
+ private HistoryPresenter presenter;
+ private LinearLayout glucoseLegend;
+ private Spinner historySpinner;
+ private BottomSheetDialog mBottomSheetDialog;
+ private Boolean isToolbarScrolling = true;
+ private int historyDropdownPosition = 0;
+
+ public static HistoryFragment newInstance() {
+ return new HistoryFragment();
+ }
+
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+
+ View mFragmentView;
+ presenter = new HistoryPresenter(this);
+ mFragmentView = inflater.inflate(R.layout.fragment_history, container, false);
+
+ mRecyclerView = mFragmentView.findViewById(R.id.fragment_history_recycler_view);
+ // use this setting to improve performance if you know that changes
+ // in content do not change the layout size of the RecyclerView
+ mLayoutManager = new LinearLayoutManager(super.getActivity());
+ mRecyclerView.setLayoutManager(mLayoutManager);
+ mRecyclerView.setHasFixedSize(false);
+ mRecyclerView.setItemAnimator(new DefaultItemAnimator());
+ historySpinner = mFragmentView.findViewById(R.id.history_spinner);
+ glucoseLegend = mFragmentView.findViewById(R.id.fragment_history_legend);
+
+ // use a linear layout manager
+ // Set array and adapter for graphSpinner
+ String[] selectorArray = getActivity().getResources().getStringArray(R.array.fragment_history_selector);
+ ArrayAdapter dataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, selectorArray);
+ dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ historySpinner.setAdapter(dataAdapter);
+
+ final Context context = getActivity().getApplicationContext();
+ historySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+ @Override
+ public void onItemSelected(AdapterView> parent, View view, int position, long id) {
+ if (!presenter.isdbEmpty()) {
+ if (position != 0) {
+ glucoseLegend.setVisibility(View.GONE);
+ } else {
+ glucoseLegend.setVisibility(View.VISIBLE);
+ }
+ mAdapter = new HistoryAdapter(context, presenter, position);
+ mRecyclerView.setAdapter(mAdapter);
+ mAdapter.notifyDataSetChanged();
+ historyDropdownPosition = position;
+ }
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView> parent) {
+
+ }
+ });
+
+ ItemClickSupport.addTo(mRecyclerView).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
+ @Override
+ public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
+ showBottomSheetDialog(v);
+ return true;
+ }
+ });
+
+ mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
+ @Override
+ public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
+ mRecyclerView.removeOnLayoutChangeListener(this);
+ updateToolbarBehaviour();
+ }
+ });
+
+ Bundle extras = getActivity().getIntent().getExtras();
+ if (extras != null) {
+ if (extras.containsKey(INTENT_EXTRA_DROPDOWN)) {
+ historySpinner.setSelection(extras.getInt(INTENT_EXTRA_DROPDOWN));
+ }
+ }
+ return mFragmentView;
+ }
+
+ private void showBottomSheetDialog(final View itemView) {
+ mBottomSheetDialog = new BottomSheetDialog(getActivity());
+ View sheetView = getActivity().getLayoutInflater().inflate(R.layout.fragment_history_bottom_sheet, null);
+ LinearLayout edit = sheetView.findViewById(R.id.fragment_history_bottom_sheet_edit);
+ LinearLayout delete = sheetView.findViewById(R.id.fragment_history_bottom_sheet_delete);
+ final TextView idTextView = itemView.findViewById(R.id.item_history_id);
+
+ edit.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ int historyTypePosition = (int) historySpinner.getSelectedItemId();
+ Intent intent;
+ switch (historyTypePosition) {
+ // HAB1C
+ case 1:
+ intent = new Intent(getActivity(), AddA1CActivity.class);
+ break;
+ // Cholesterol
+ case 2:
+ intent = new Intent(getActivity(), AddCholesterolActivity.class);
+ break;
+ // Pressure
+ case 3:
+ intent = new Intent(getActivity(), AddPressureActivity.class);
+ break;
+ // Ketone
+ case 4:
+ intent = new Intent(getActivity(), AddKetoneActivity.class);
+ break;
+ // Weight
+ case 5:
+ intent = new Intent(getActivity(), AddWeightActivity.class);
+ break;
+ // Glucose
+ default:
+ intent = new Intent(getActivity(), AddGlucoseActivity.class);
+ break;
+ }
+
+ intent.putExtra(INTENT_EXTRA_EDITING_ID, Long.parseLong(idTextView.getText().toString()));
+ intent.putExtra(INTENT_EXTRA_EDITING, true);
+ intent.putExtra(INTENT_EXTRA_DROPDOWN, historyDropdownPosition);
+ // History page is 1
+ intent.putExtra(INTENT_EXTRA_PAGER, 1);
+ startActivity(intent);
+ mBottomSheetDialog.dismiss();
+ getActivity().finish();
+ }
+ });
+
+ delete.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ mBottomSheetDialog.dismiss();
+ TextView idTextView = itemView.findViewById(R.id.item_history_id);
+ final long idToDelete = Long.parseLong(idTextView.getText().toString());
+ final CardView item = itemView.findViewById(R.id.item_history);
+ item.animate().alpha(0.0f).setDuration(2000);
+ Snackbar.make(((MainActivity) getActivity()).getFabView(), R.string.fragment_history_snackbar_text, Snackbar.LENGTH_SHORT).setCallback(new Snackbar.Callback() {
+ @Override
+ public void onDismissed(Snackbar snackbar, int event) {
+ switch (event) {
+ case Snackbar.Callback.DISMISS_EVENT_ACTION:
+ // Do nothing, see Undo onClickListener
+ break;
+ case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
+ presenter.onDeleteClicked(idToDelete, historySpinner.getSelectedItemPosition());
+ break;
+ default:
+ break;
+ }
+ }
+ }).setAction("UNDO", new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ item.clearAnimation();
+ item.setAlpha(1.0f);
+ mAdapter.notifyDataSetChanged();
+ }
+ }).setActionTextColor(ContextCompat.getColor(getContext(), R.color.glucosio_accent)).show();
+
+ }
+ });
+
+ mBottomSheetDialog.setContentView(sheetView);
+ mBottomSheetDialog.show();
+ mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+ @Override
+ public void onDismiss(DialogInterface dialog) {
+ mBottomSheetDialog = null;
+ }
+ });
+ }
+
+ public void updateToolbarBehaviour() {
+ if (mAdapter != null) {
+ if (mLayoutManager.findLastCompletelyVisibleItemPosition() == mAdapter.getItemCount() - 1) {
+ isToolbarScrolling = false;
+ if (getActivity() != null) {
+ ((MainActivity) getActivity()).turnOffToolbarScrolling();
+ }
+ } else {
+ if (!isToolbarScrolling) {
+ isToolbarScrolling = true;
+ ((MainActivity) getActivity()).turnOnToolbarScrolling();
+ }
+ }
+ }
+ }
+
+ public String convertDate(String date) {
+ FormatDateTime dateTime = new FormatDateTime(getActivity().getApplicationContext());
+ return dateTime.convertDateTime(date);
+ }
+
+ public void notifyAdapter() {
+ mAdapter.notifyDataSetChanged();
+ }
+
+ public void reloadFragmentAdapter() {
+ if (getActivity() != null) {
+ ((MainActivity) getActivity()).reloadFragmentAdapter();
+ ((MainActivity) getActivity()).checkIfEmptyLayout();
+ }
+ }
+
+ public int getHistoryDropdownPosition() {
+ return historyDropdownPosition;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java b/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java
new file mode 100644
index 0000000..9981240
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java
@@ -0,0 +1,748 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.fragment;
+
+import android.Manifest;
+import android.app.Dialog;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+import android.graphics.Color;
+import android.os.Build;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.support.annotation.NonNull;
+import android.support.design.widget.Snackbar;
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.app.Fragment;
+import android.support.v4.content.ContextCompat;
+import android.view.LayoutInflater;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.ImageButton;
+import android.widget.ListView;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+import com.github.mikephil.charting.animation.Easing;
+import com.github.mikephil.charting.charts.LineChart;
+import com.github.mikephil.charting.components.AxisBase;
+import com.github.mikephil.charting.components.Legend;
+import com.github.mikephil.charting.components.LimitLine;
+import com.github.mikephil.charting.components.XAxis;
+import com.github.mikephil.charting.components.YAxis;
+import com.github.mikephil.charting.data.Entry;
+import com.github.mikephil.charting.data.LineData;
+import com.github.mikephil.charting.data.LineDataSet;
+import com.github.mikephil.charting.formatter.IAxisValueFormatter;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.GlucosioApplication;
+import org.glucosio.android.R;
+import org.glucosio.android.adapter.A1cEstimateAdapter;
+import org.glucosio.android.presenter.OverviewPresenter;
+import org.glucosio.android.tools.FormatDateTime;
+import org.glucosio.android.tools.GlucoseRanges;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.ReadingTools;
+import org.glucosio.android.tools.TipsManager;
+import org.glucosio.android.view.OverviewView;
+
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+public class OverviewFragment extends Fragment implements OverviewView {
+
+ private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
+ private ImageButton HB1ACMoreButton;
+ private LineChart chart;
+ private TextView lastReadingTextView;
+ private TextView lastDateTextView;
+ private TextView tipTextView;
+ private TextView HB1ACTextView;
+ private TextView HB1ACDateTextView;
+ private Spinner graphSpinnerRange;
+ private OverviewPresenter presenter;
+
+ private CheckBox graphCheckboxGlucose;
+ private CheckBox graphCheckboxKetones;
+ private CheckBox graphCheckboxCholesterol;
+ private CheckBox graphCheckboxA1c;
+ private CheckBox graphCheckboxWeight;
+ private CheckBox graphCheckboxPressure;
+ private View mFragmentView;
+
+ private List xValues = new ArrayList<>();
+
+
+ public static OverviewFragment newInstance() {
+ return new OverviewFragment();
+ }
+
+ public static void disableTouchTheft(View view) {
+ view.setOnTouchListener(new View.OnTouchListener() {
+ @Override
+ public boolean onTouch(View view, MotionEvent motionEvent) {
+ view.getParent().requestDisallowInterceptTouchEvent(true);
+ switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
+ case MotionEvent.ACTION_UP:
+ view.getParent().requestDisallowInterceptTouchEvent(false);
+ break;
+ }
+ return false;
+ }
+ });
+ }
+
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ GlucosioApplication app = (GlucosioApplication) getActivity().getApplicationContext();
+ presenter = new OverviewPresenter(this, app.getDBHandler());
+ if (!presenter.isdbEmpty()) {
+ presenter.loadDatabase(isNewGraphEnabled());
+ }
+
+ mFragmentView = inflater.inflate(R.layout.fragment_overview, container, false);
+
+ chart = mFragmentView.findViewById(R.id.chart);
+ disableTouchTheft(chart);
+ Legend legend = chart.getLegend();
+
+ lastReadingTextView = mFragmentView.findViewById(R.id.item_history_reading);
+ lastDateTextView = mFragmentView.findViewById(R.id.fragment_overview_last_date);
+ tipTextView = mFragmentView.findViewById(R.id.random_tip_textview);
+ graphSpinnerRange = mFragmentView.findViewById(R.id.chart_spinner_range);
+ Spinner graphSpinnerMetric = mFragmentView.findViewById(R.id.chart_spinner_metrics);
+ ImageButton graphExport = mFragmentView.findViewById(R.id.fragment_overview_graph_export);
+ HB1ACTextView = mFragmentView.findViewById(R.id.fragment_overview_hb1ac);
+ HB1ACDateTextView = mFragmentView.findViewById(R.id.fragment_overview_hb1ac_date);
+ HB1ACMoreButton = mFragmentView.findViewById(R.id.fragment_overview_a1c_more);
+ graphCheckboxGlucose = mFragmentView.findViewById(R.id.fragment_overview_graph_glucose);
+ graphCheckboxKetones = mFragmentView.findViewById(R.id.fragment_overview_graph_ketones);
+ graphCheckboxCholesterol = mFragmentView.findViewById(R.id.fragment_overview_graph_cholesterol);
+ graphCheckboxA1c = mFragmentView.findViewById(R.id.fragment_overview_graph_a1c);
+ graphCheckboxWeight = mFragmentView.findViewById(R.id.fragment_overview_graph_weight);
+ graphCheckboxPressure = mFragmentView.findViewById(R.id.fragment_overview_graph_pressure);
+
+ graphCheckboxGlucose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxCholesterol.setChecked(false);
+ graphCheckboxKetones.setChecked(false);
+ graphCheckboxPressure.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxA1c.setChecked(false);
+ graphCheckboxGlucose.setChecked(b);
+ }
+ });
+
+ graphCheckboxA1c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxGlucose.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxCholesterol.setChecked(false);
+ graphCheckboxKetones.setChecked(false);
+ graphCheckboxPressure.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphSpinnerRange.setEnabled(!b);
+ graphCheckboxA1c.setChecked(b);
+ }
+ });
+
+ graphCheckboxKetones.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxGlucose.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxCholesterol.setChecked(false);
+ graphCheckboxPressure.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxA1c.setChecked(false);
+ graphSpinnerRange.setEnabled(!b);
+ graphCheckboxKetones.setChecked(b);
+ }
+ });
+
+ graphCheckboxWeight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxGlucose.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxCholesterol.setChecked(false);
+ graphCheckboxKetones.setChecked(false);
+ graphCheckboxPressure.setChecked(false);
+ graphCheckboxA1c.setChecked(false);
+ graphSpinnerRange.setEnabled(!b);
+ graphCheckboxWeight.setChecked(b);
+ }
+ });
+
+ graphCheckboxPressure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxGlucose.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxCholesterol.setChecked(false);
+ graphCheckboxKetones.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxA1c.setChecked(false);
+ graphSpinnerRange.setEnabled(!b);
+ graphCheckboxPressure.setChecked(b);
+ }
+ });
+
+ graphCheckboxCholesterol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ setData();
+ graphCheckboxGlucose.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxKetones.setChecked(false);
+ graphCheckboxPressure.setChecked(false);
+ graphCheckboxWeight.setChecked(false);
+ graphCheckboxA1c.setChecked(false);
+ graphSpinnerRange.setEnabled(!b);
+ graphCheckboxCholesterol.setChecked(b);
+ }
+ });
+
+ HB1ACMoreButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ showA1cDialog();
+ }
+ });
+ // Set array and adapter for graphSpinnerRange
+ String[] selectorRangeArray = getActivity().getResources().getStringArray(R.array.fragment_overview_selector_range);
+ String[] selectorMetricArray = getActivity().getResources().getStringArray(R.array.fragment_overview_selector_metric);
+ ArrayAdapter dataRangeAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, selectorRangeArray);
+ ArrayAdapter dataMetricAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, selectorMetricArray);
+ dataRangeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ dataMetricAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ graphSpinnerRange.setAdapter(dataRangeAdapter);
+ graphSpinnerMetric.setAdapter(dataMetricAdapter);
+
+ graphSpinnerRange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+ @Override
+ public void onItemSelected(AdapterView> parent, View view, int position, long id) {
+ if (!presenter.isdbEmpty()) {
+ setData();
+ }
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView> parent) {
+
+ }
+ });
+
+
+ final XAxis xAxis = chart.getXAxis();
+ xAxis.setDrawGridLines(false);
+ xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
+ xAxis.setTextColor(ContextCompat.getColor(getContext(), R.color.glucosio_text_light));
+ xAxis.setAvoidFirstLastClipping(true);
+
+ double minGlucoseValue = presenter.getGlucoseMinValue();
+ double maxGlucoseValue = presenter.getGlucoseMaxValue();
+
+ LimitLine ll1;
+ LimitLine ll2;
+
+ if (Constants.Units.MG_DL.equals(presenter.getUnitMeasurement())) {
+ ll1 = new LimitLine((float) minGlucoseValue);
+ ll2 = new LimitLine((float) maxGlucoseValue);
+ } else {
+ ll1 = new LimitLine((float) GlucosioConverter.glucoseToMmolL(maxGlucoseValue), getString(R.string.reading_high));
+ ll2 = new LimitLine((float) GlucosioConverter.glucoseToMmolL(minGlucoseValue), getString(R.string.reading_low));
+ }
+
+ ll1.setLineWidth(0.8f);
+ ll1.setLineColor(ContextCompat.getColor(getContext(), R.color.glucosio_reading_low));
+
+ ll2.setLineWidth(0.8f);
+ ll2.setLineColor(ContextCompat.getColor(getContext(), R.color.glucosio_reading_high));
+
+ YAxis leftAxis = chart.getAxisLeft();
+ leftAxis.setTextColor(ContextCompat.getColor(getContext(), R.color.glucosio_text_light));
+ leftAxis.setStartAtZero(false);
+ leftAxis.disableGridDashedLine();
+ leftAxis.setDrawGridLines(false);
+ leftAxis.addLimitLine(ll1);
+ leftAxis.addLimitLine(ll2);
+ leftAxis.setDrawLimitLinesBehindData(true);
+
+ chart.getAxisRight().setEnabled(false);
+ chart.setBackgroundColor(Color.parseColor("#FFFFFF"));
+ chart.setGridBackgroundColor(Color.parseColor("#FFFFFF"));
+ if (!presenter.isdbEmpty()) {
+ setData();
+ }
+ legend.setEnabled(false);
+
+ graphExport.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ if (ContextCompat.checkSelfPermission(getActivity(),
+ Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ != PackageManager.PERMISSION_GRANTED) {
+ // If we don't have permission, ask the user
+
+ ActivityCompat.requestPermissions(getActivity(),
+ new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+ PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
+
+ Snackbar.make(mFragmentView, getString(R.string.fragment_overview_permission_storage), Snackbar.LENGTH_SHORT).show();
+ } else {
+ // else save the image to gallery
+ exportGraphToGallery();
+ }
+ }
+ });
+
+ loadLastReading();
+ loadHB1AC();
+ loadRandomTip();
+
+ return mFragmentView;
+ }
+
+ private void exportGraphToGallery() {
+ long timestamp = System.currentTimeMillis() / 1000;
+ boolean saved = chart.saveToGallery("glucosio_" + timestamp, 50);
+ if (saved) {
+ Snackbar.make(mFragmentView, R.string.fragment_overview_graph_export_true, Snackbar.LENGTH_SHORT).show();
+ } else {
+ Snackbar.make(mFragmentView, R.string.fragment_overview_graph_export_false, Snackbar.LENGTH_SHORT).show();
+ }
+ }
+
+ private void setData() {
+ LineData data = new LineData();
+ if (graphCheckboxGlucose.isChecked()) {
+ data = generateGlucoseData();
+ }
+
+ if (graphCheckboxA1c.isChecked()) {
+ data = generateA1cData();
+ }
+
+ if (graphCheckboxKetones.isChecked()) {
+ data = generateKetonesData();
+ }
+
+ if (graphCheckboxWeight.isChecked()) {
+ data = generateWeightData();
+ }
+
+ if (graphCheckboxPressure.isChecked()) {
+ data = generatePressureData();
+ }
+
+ if (graphCheckboxCholesterol.isChecked()) {
+ data = generateCholesterolData();
+ }
+
+
+ if (data.getEntryCount() != 0) {
+ chart.setData(data);
+ } else {
+ chart.setData(null);
+ }
+ chart.setPinchZoom(true);
+ chart.setHardwareAccelerationEnabled(true);
+ chart.setNoDataTextColor(getResources().getColor(R.color.glucosio_text));
+ chart.animateY(1000, Easing.EasingOption.EaseOutCubic);
+ chart.invalidate();
+ chart.notifyDataSetChanged();
+ chart.fitScreen();
+ chart.setDescription(null);
+ chart.setVisibleXRangeMaximum(20);
+ chart.moveViewToX(data.getXMax());
+
+ XAxis xAxis = chart.getXAxis();
+
+ final LineData finalData = data;
+ IAxisValueFormatter formatter = new IAxisValueFormatter() {
+
+ @Override
+ public String getFormattedValue(float value, AxisBase axis) {
+ // Dirty fix for a library bug. I have to report it online because 'value' returns old values even if the dataset is changed
+ if (value < xValues.size() && value > 0) {
+ return xValues.get((int) value);
+ } else {
+ return "";
+ }
+ }
+ };
+ xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
+ xAxis.setValueFormatter(formatter);
+
+ }
+
+ private LineData generateGlucoseData() {
+ List xVals = new ArrayList<>();
+ List yVals = new ArrayList<>();
+
+ if (graphSpinnerRange.getSelectedItemPosition() == 0) {
+ List glucosioReadings = presenter.getGlucoseReadings();
+
+ // Day view
+ for (int i = 0; i < glucosioReadings.size(); i++) {
+ if (presenter.getUnitMeasurement().equals(Constants.Units.MG_DL)) {
+ float val = glucosioReadings.get(i).floatValue();
+ yVals.add(new Entry(i, val));
+ } else {
+ double val = GlucosioConverter.glucoseToMmolL(glucosioReadings.get(i));
+ float converted = (float) val;
+ yVals.add(new Entry(i, converted));
+ }
+ }
+ } else if (graphSpinnerRange.getSelectedItemPosition() == 1) {
+ List glucosioReadingsWeek = presenter.getGlucoseReadingsWeek();
+ // Week view
+ for (int i = 0; i < presenter.getGlucoseReadingsWeek().size(); i++) {
+ if (presenter.getUnitMeasurement().equals(Constants.Units.MG_DL)) {
+ float val = glucosioReadingsWeek.get(i).floatValue();
+ yVals.add(new Entry(i, val));
+ } else {
+ double val = GlucosioConverter.glucoseToMmolL(glucosioReadingsWeek.get(i));
+ float converted = (float) val;
+ yVals.add(new Entry(i, converted));
+ }
+ }
+ } else {
+ List glucosioReadingsMonth = presenter.getGlucoseReadingsMonth();
+ // Month view
+ for (int i = 0; i < presenter.getGlucoseReadingsMonth().size(); i++) {
+ if (presenter.getUnitMeasurement().equals(Constants.Units.MG_DL)) {
+ float val = glucosioReadingsMonth.get(i).floatValue();
+ yVals.add(new Entry(i, val));
+ } else {
+ double val = GlucosioConverter.glucoseToMmolL(ReadingTools.safeParseDouble(glucosioReadingsMonth.get(i) + ""));
+ float converted = (float) val;
+ yVals.add(new Entry(i, converted));
+ }
+ }
+ }
+
+ if (graphSpinnerRange.getSelectedItemPosition() == 0) {
+ // Day view
+ for (int i = 0; i < presenter.getGraphGlucoseDateTime().size(); i++) {
+ String date = presenter.convertDate(presenter.getGraphGlucoseDateTime().get(i));
+ xVals.add(date);
+ }
+ } else if (graphSpinnerRange.getSelectedItemPosition() == 1) {
+ // Week view
+ for (int i = 0; i < presenter.getGlucoseReadingsWeek().size(); i++) {
+ String date = presenter.convertDate(presenter.getGlucoseDatetimeWeek().get(i));
+ xVals.add(date);
+ }
+ } else {
+ // Month view
+ for (int i = 0; i < presenter.getGlucoseReadingsMonth().size(); i++) {
+ String date = presenter.convertDateToMonth(presenter.getGlucoseDatetimeMonth().get(i));
+ xVals.add(date);
+ }
+ }
+
+ xValues = xVals;
+ LineData data = new LineData(generateLineDataSet(yVals, ContextCompat.getColor(getContext(), R.color.glucosio_pink)));
+ return data;
+ }
+
+ private LineData generateA1cData() {
+ ArrayList xVals = new ArrayList<>();
+ ArrayList yVals = new ArrayList<>();
+
+ int k = 0;
+ for (int i = presenter.getA1cReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getA1cReadings().get(i).toString());
+ yVals.add(new Entry(k, val));
+ k++;
+ }
+
+ for (int i = presenter.getA1cReadingsDateTime().size() - 1; i >= 0; i--) {
+ String date = presenter.convertDate(presenter.getA1cReadingsDateTime().get(i));
+ xVals.add(date);
+ }
+
+ xValues = xVals;
+ // create a data object with the datasets
+ return new LineData(generateLineDataSet(yVals, ContextCompat.getColor(getContext(), R.color.glucosio_fab_HB1AC)));
+ }
+
+ private LineData generateKetonesData() {
+ List xVals = new ArrayList<>();
+ List yVals = new ArrayList<>();
+
+ int k = 0;
+ for (int i = presenter.getKetonesReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getKetonesReadings().get(i).toString());
+ yVals.add(new Entry(k, val));
+ k++;
+ }
+
+ for (int i = presenter.getKetonesReadingsDateTime().size() - 1; i >= 0; i--) {
+ String date = presenter.convertDate(presenter.getKetonesReadingsDateTime().get(i));
+ xVals.add(date);
+ }
+
+ xValues = xVals;
+ // create a data object with the datasets
+ return new LineData(generateLineDataSet(yVals, ContextCompat.getColor(getContext(), R.color.glucosio_fab_ketones)));
+ }
+
+ private LineData generateWeightData() {
+ List xVals = new ArrayList<>();
+ List yVals = new ArrayList<>();
+ boolean needUnitConversion = false;
+
+ if (!"kilograms".equals(presenter.getWeightUnitMeasuerement())) {
+ needUnitConversion = true;
+ }
+
+ int k = 0;
+ for (int i = presenter.getWeightReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getWeightReadings().get(i).toString());
+ if (needUnitConversion) {
+ val = (float) GlucosioConverter.kgToLb(val);
+ }
+
+ yVals.add(new Entry(k, val));
+ k++;
+ }
+
+ for (int i = presenter.getWeightReadingsDateTime().size() - 1; i >= 0; i--) {
+ String date = presenter.convertDate(presenter.getWeightReadingsDateTime().get(i));
+ xVals.add(date);
+ }
+
+ xValues = xVals;
+ // create a data object with the datasets
+ return new LineData(generateLineDataSet(yVals, ContextCompat.getColor(getContext(), R.color.glucosio_fab_weight)));
+ }
+
+ private LineData generatePressureData() {
+ List xVals = new ArrayList<>();
+ List yValsMax = new ArrayList<>();
+ List yValsMin = new ArrayList<>();
+
+ int k = 0;
+ for (int i = presenter.getMaxPressureReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getMaxPressureReadings().get(i).toString());
+ yValsMax.add(new Entry(k, val));
+ k++;
+ }
+
+ int j = 0;
+ for (int i = presenter.getMinPressureReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getMinPressureReadings().get(i).toString());
+ yValsMin.add(new Entry(j, val));
+ j++;
+ }
+
+ for (int i = presenter.getPressureReadingsDateTime().size() - 1; i >= 0; i--) {
+ String date = presenter.convertDate(presenter.getPressureReadingsDateTime().get(i));
+ xVals.add(date);
+ }
+
+ xValues = xVals;
+ LineData data = new LineData(generateLineDataSet(yValsMax, ContextCompat.getColor(getContext(), R.color.glucosio_fab_pressure)));
+ data.addDataSet(generateLineDataSet(yValsMin, ContextCompat.getColor(getContext(), R.color.glucosio_fab_pressure)));
+ // create a data object with the datasets
+ return data;
+ }
+
+ private LineData generateCholesterolData() {
+ List xVals = new ArrayList<>();
+ List yVals = new ArrayList<>();
+
+ int k = 0;
+ for (int i = presenter.getCholesterolReadings().size() - 1; i >= 0; i--) {
+ float val = Float.parseFloat(presenter.getCholesterolReadings().get(i).toString());
+ yVals.add(new Entry(k, val));
+ k++;
+ }
+
+ for (int i = presenter.getCholesterolReadingsDateTime().size() - 1; i >= 0; i--) {
+ String date = presenter.convertDate(presenter.getCholesterolReadingsDateTime().get(i));
+ xVals.add(date);
+ }
+
+ xValues = xVals;
+ // create a data object with the datasets
+ return new LineData(generateLineDataSet(yVals, ContextCompat.getColor(getContext(), R.color.glucosio_fab_cholesterol)));
+ }
+
+ private LineDataSet generateLineDataSet(List vals, int color) {
+ // create a dataset and give it a type
+ LineDataSet set1 = new LineDataSet(vals, "");
+ List colors = new ArrayList<>();
+
+ if (color == ContextCompat.getColor(getContext(), R.color.glucosio_pink)) {
+ for (Entry val : vals) {
+ if (val.getY() == (0)) {
+ colors.add(Color.TRANSPARENT);
+ } else {
+ colors.add(color);
+ }
+ }
+ set1.setCircleColors(colors);
+ } else {
+ set1.setCircleColor(color);
+ }
+
+ set1.setColor(color);
+ set1.setLineWidth(2f);
+ set1.setCircleSize(4f);
+ set1.setDrawCircleHole(true);
+ set1.disableDashedLine();
+ set1.setFillAlpha(255);
+ set1.setDrawFilled(true);
+ set1.setValueTextSize(0);
+ set1.setValueTextColor(Color.parseColor("#FFFFFF"));
+ set1.setFillDrawable(getResources().getDrawable(R.drawable.graph_gradient));
+ set1.setHighLightColor(ContextCompat.getColor(getContext(), R.color.glucosio_gray_light));
+ set1.setCubicIntensity(0.2f);
+
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+ set1.setDrawFilled(false);
+ set1.setLineWidth(2f);
+ set1.setCircleSize(4f);
+ set1.setDrawCircleHole(true);
+ }
+ return set1;
+ }
+
+ private void showA1cDialog() {
+ final Dialog a1CDialog = new Dialog(getActivity(), R.style.GlucosioTheme);
+
+ WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
+ lp.copyFrom(a1CDialog.getWindow().getAttributes());
+ a1CDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
+ lp.width = WindowManager.LayoutParams.MATCH_PARENT;
+ lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
+ a1CDialog.setContentView(R.layout.dialog_a1c);
+ a1CDialog.getWindow().setAttributes(lp);
+ a1CDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
+ a1CDialog.getWindow().setDimAmount(0.5f);
+ a1CDialog.setCanceledOnTouchOutside(true);
+ a1CDialog.show();
+
+ ListView a1cListView = a1CDialog.findViewById(R.id.dialog_a1c_listview);
+
+ A1cEstimateAdapter customAdapter = new A1cEstimateAdapter(
+ getActivity(), R.layout.dialog_a1c_item, presenter.getA1cEstimateList());
+
+ a1cListView.setAdapter(customAdapter);
+ }
+
+ private void loadHB1AC() {
+ if (!presenter.isdbEmpty()) {
+ HB1ACTextView.setText(presenter.getHB1AC());
+ HB1ACDateTextView.setText(presenter.getA1cMonth());
+ // We show the A1C more button only if 2 or more A1C estimates are available
+ if (!presenter.isA1cAvailable(2)) {
+ HB1ACMoreButton.setVisibility(View.GONE);
+ }
+ }
+ }
+
+ private void loadLastReading() {
+ if (!presenter.isdbEmpty()) {
+ if (presenter.getUnitMeasurement().equals(Constants.Units.MG_DL)) {
+ String reading = presenter.getLastReading();
+ lastReadingTextView.setText(getString(R.string.mg_dL_value, reading));
+ } else {
+ String mgdl = presenter.getLastReading();
+ double mmol = GlucosioConverter.glucoseToMmolL(ReadingTools.safeParseDouble(mgdl));
+ String reading = NumberFormat.getInstance().format(mmol);
+ lastReadingTextView.setText(getString(R.string.mmol_L_value, reading));
+ }
+
+ FormatDateTime dateTime = new FormatDateTime(getActivity().getApplicationContext());
+ lastDateTextView.setText(dateTime.convertDateTime(presenter.getLastDateTime()));
+
+ GlucoseRanges ranges = new GlucoseRanges(getActivity().getApplicationContext());
+ String color = ranges.colorFromReading(ReadingTools.safeParseDouble(presenter.getLastReading()));
+ lastReadingTextView.setTextColor(ranges.stringToColor(color));
+ }
+ }
+
+ private void loadRandomTip() {
+ TipsManager tipsManager = new TipsManager(getActivity().getApplicationContext(), presenter.getUserAge());
+ tipTextView.setText(presenter.getRandomTip(tipsManager));
+ }
+
+ private boolean isNewGraphEnabled() {
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
+ return !sharedPref.getBoolean("pref_graph_old", false);
+ }
+
+ @NonNull
+ public String convertDate(@NonNull final String date) {
+ FormatDateTime dateTime = new FormatDateTime(getActivity().getApplicationContext());
+ return dateTime.convertDate(date);
+ }
+
+ @NonNull
+ public String convertDateToMonth(@NonNull final String date) {
+ FormatDateTime dateTime = new FormatDateTime((getActivity().getApplication()));
+ return dateTime.convertDateToMonthOverview(date);
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
+ switch (requestCode) {
+ case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
+ // If request is cancelled, the result arrays are empty.
+ if (grantResults.length > 0
+ && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ // permission was granted, yay!
+ exportGraphToGallery();
+ } else {
+ Snackbar.make(mFragmentView, R.string.fragment_overview_permission_storage, Snackbar.LENGTH_LONG).show();
+ }
+ }
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/listener/ItemClickSupport.java b/app/src/main/java/org/glucosio/android/listener/ItemClickSupport.java
new file mode 100644
index 0000000..c429dba
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/listener/ItemClickSupport.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.listener;
+
+import android.support.v7.widget.RecyclerView;
+import android.view.View;
+
+import org.glucosio.android.R;
+
+
+/**
+ * @see Getting your clicks on RecyclerView | Hugo Visser
+ */
+public class ItemClickSupport {
+ private final RecyclerView mRecyclerView;
+ private OnItemClickListener mOnItemClickListener;
+ private OnItemLongClickListener mOnItemLongClickListener;
+ private View.OnClickListener mOnClickListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ if (mOnItemClickListener != null) {
+ RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
+ mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
+ }
+ }
+ };
+ private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
+ @Override
+ public boolean onLongClick(View v) {
+ if (mOnItemLongClickListener != null) {
+ RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
+ return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
+ }
+ return false;
+ }
+ };
+ private RecyclerView.OnChildAttachStateChangeListener mAttachListener
+ = new RecyclerView.OnChildAttachStateChangeListener() {
+ @Override
+ public void onChildViewAttachedToWindow(View view) {
+ if (mOnItemClickListener != null) {
+ view.setOnClickListener(mOnClickListener);
+ }
+ if (mOnItemLongClickListener != null) {
+ view.setOnLongClickListener(mOnLongClickListener);
+ }
+ }
+
+ @Override
+ public void onChildViewDetachedFromWindow(View view) {
+
+ }
+ };
+
+ private ItemClickSupport(RecyclerView recyclerView) {
+ mRecyclerView = recyclerView;
+ mRecyclerView.setTag(R.id.item_click_support, this);
+ mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
+ }
+
+ public static ItemClickSupport addTo(RecyclerView view) {
+ ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
+ if (support == null) {
+ support = new ItemClickSupport(view);
+ }
+ return support;
+ }
+
+ public static ItemClickSupport removeFrom(RecyclerView view) {
+ ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
+ if (support != null) {
+ support.detach(view);
+ }
+ return support;
+ }
+
+ public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
+ mOnItemClickListener = listener;
+ return this;
+ }
+
+ public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
+ mOnItemLongClickListener = listener;
+ return this;
+ }
+
+ private void detach(RecyclerView view) {
+ view.removeOnChildAttachStateChangeListener(mAttachListener);
+ view.setTag(R.id.item_click_support, null);
+ }
+
+ interface OnItemClickListener {
+
+ void onItemClicked(RecyclerView recyclerView, int position, View v);
+ }
+
+ public interface OnItemLongClickListener {
+
+ boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/A1cEstimate.java b/app/src/main/java/org/glucosio/android/object/A1cEstimate.java
new file mode 100644
index 0000000..2ef90c8
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/A1cEstimate.java
@@ -0,0 +1,35 @@
+package org.glucosio.android.object;
+
+import org.glucosio.android.tools.GlucosioConverter;
+
+import java.text.NumberFormat;
+
+public class A1cEstimate {
+ private double value;
+ private String month;
+
+ public A1cEstimate(double value, String month) {
+ this.value = value;
+ this.month = month;
+ }
+
+ public double getValue() {
+ return value;
+ }
+
+ public void setValue(double value) {
+ this.value = value;
+ }
+
+ public String getMonth() {
+ return month;
+ }
+
+ public void setMonth(String month) {
+ this.month = month;
+ }
+
+ public String getGlucoseAverage() {
+ return NumberFormat.getInstance().format(GlucosioConverter.a1cToGlucose(value));
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/ActionTip.java b/app/src/main/java/org/glucosio/android/object/ActionTip.java
new file mode 100644
index 0000000..bb88c3f
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/ActionTip.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.object;
+
+/**
+ * Created by paolo on 24/10/15.
+ */
+public class ActionTip {
+
+ private String tipTitle;
+ private String tipDescription;
+ private String tipAction;
+
+ public String getTipTitle() {
+ return tipTitle;
+ }
+
+ public void setTipTitle(String tipTitle) {
+ this.tipTitle = tipTitle;
+ }
+
+ public String getTipDescription() {
+ return tipDescription;
+ }
+
+ public void setTipDescription(String tipDescription) {
+ this.tipDescription = tipDescription;
+ }
+
+ public String getTipAction() {
+ return tipAction;
+ }
+
+ public void setTipAction(String tipAction) {
+ this.tipAction = tipAction;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/DoubleGraphObject.java b/app/src/main/java/org/glucosio/android/object/DoubleGraphObject.java
new file mode 100644
index 0000000..ef5e554
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/DoubleGraphObject.java
@@ -0,0 +1,29 @@
+package org.glucosio.android.object;
+
+import org.joda.time.DateTime;
+
+public class DoubleGraphObject {
+ private DateTime created;
+ private double reading;
+
+ public DoubleGraphObject(DateTime created, double reading) {
+ this.created = created;
+ this.reading = reading;
+ }
+
+ public DateTime getCreated() {
+ return created;
+ }
+
+ public void setCreated(DateTime created) {
+ this.created = created;
+ }
+
+ public double getReading() {
+ return reading;
+ }
+
+ public void setReading(int reading) {
+ this.reading = reading;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/GlucoseData.java b/app/src/main/java/org/glucosio/android/object/GlucoseData.java
new file mode 100644
index 0000000..4b00a61
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/GlucoseData.java
@@ -0,0 +1,28 @@
+package org.glucosio.android.object;
+
+import java.text.DecimalFormat;
+
+public class GlucoseData implements Comparable {
+
+ public long realDate;
+ public String sensorId;
+ public long sensorTime;
+ public int glucoseLevel = -1;
+ public long phoneDatabaseId;
+
+ public GlucoseData() {
+ }
+
+ public static String glucose(int mgdl, boolean mmol) {
+ return mmol ? new DecimalFormat("##.0").format(mgdl / 18f) : String.valueOf(mgdl);
+ }
+
+ public String glucose(boolean mmol) {
+ return glucose(glucoseLevel, mmol);
+ }
+
+ @Override
+ public int compareTo(GlucoseData another) {
+ return (int) (realDate - another.realDate);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/GlucosioBackup.java b/app/src/main/java/org/glucosio/android/object/GlucosioBackup.java
new file mode 100644
index 0000000..864bbab
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/GlucosioBackup.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.object;
+
+import com.google.android.gms.drive.DriveId;
+
+import java.util.Date;
+
+
+public class GlucosioBackup {
+
+ private DriveId driveId;
+ private Date modifiedDate;
+ private long backupSize;
+
+ public GlucosioBackup(DriveId driveId, Date modifiedDate, long backupSize) {
+ this.driveId = driveId;
+ this.modifiedDate = modifiedDate;
+ this.backupSize = backupSize;
+ }
+
+ public DriveId getDriveId() {
+ return driveId;
+ }
+
+ public void setDriveId(DriveId driveId) {
+ this.driveId = driveId;
+ }
+
+ public Date getModifiedDate() {
+ return modifiedDate;
+ }
+
+ public void setModifiedDate(Date modifiedDate) {
+ this.modifiedDate = modifiedDate;
+ }
+
+ public long getBackupSize() {
+ return backupSize;
+ }
+
+ public void setBackupSize(long backupSize) {
+ this.backupSize = backupSize;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/IntGraphObject.java b/app/src/main/java/org/glucosio/android/object/IntGraphObject.java
new file mode 100644
index 0000000..ce6451a
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/IntGraphObject.java
@@ -0,0 +1,29 @@
+package org.glucosio.android.object;
+
+import org.joda.time.DateTime;
+
+public class IntGraphObject {
+ private DateTime created;
+ private int reading;
+
+ public IntGraphObject(DateTime created, int reading) {
+ this.created = created;
+ this.reading = reading;
+ }
+
+ public DateTime getCreated() {
+ return created;
+ }
+
+ public void setCreated(DateTime created) {
+ this.created = created;
+ }
+
+ public int getReading() {
+ return reading;
+ }
+
+ public void setReading(int reading) {
+ this.reading = reading;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/object/PredictionData.java b/app/src/main/java/org/glucosio/android/object/PredictionData.java
new file mode 100644
index 0000000..0b0d59f
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/PredictionData.java
@@ -0,0 +1,19 @@
+package org.glucosio.android.object;
+
+public class PredictionData extends GlucoseData {
+
+ public double trend = -1;
+ public double confidence = -1;
+ public Result errorCode;
+ public int attempt;
+
+ public PredictionData() {
+ }
+
+ public enum Result {
+ OK,
+ ERROR_NO_NFC,
+ ERROR_NFC_READ
+ }
+
+}
diff --git a/app/src/main/java/org/glucosio/android/object/ReadingData.java b/app/src/main/java/org/glucosio/android/object/ReadingData.java
new file mode 100644
index 0000000..362ada6
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/object/ReadingData.java
@@ -0,0 +1,41 @@
+package org.glucosio.android.object;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ReadingData {
+
+ public PredictionData prediction;
+ public List trend;
+ public List history;
+
+ public ReadingData(PredictionData.Result result) {
+ this.prediction = new PredictionData();
+ this.prediction.realDate = System.currentTimeMillis();
+ this.prediction.errorCode = result;
+ this.trend = new ArrayList<>();
+ this.history = new ArrayList<>();
+ }
+
+ public ReadingData(PredictionData prediction, List trend, List history) {
+ this.prediction = prediction;
+ this.trend = trend;
+ this.history = history;
+ }
+
+ public ReadingData() {
+ }
+
+ public static class TransferObject {
+ public long id;
+ public ReadingData data;
+
+ public TransferObject() {
+ }
+
+ public TransferObject(long id, ReadingData data) {
+ this.id = id;
+ this.data = data;
+ }
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/A1CCalculatorPresenter.java b/app/src/main/java/org/glucosio/android/presenter/A1CCalculatorPresenter.java
new file mode 100644
index 0000000..04482c6
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/A1CCalculatorPresenter.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.support.annotation.NonNull;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.activity.A1cCalculatorActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.HB1ACReading;
+import org.glucosio.android.db.User;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class A1CCalculatorPresenter {
+ private final DatabaseHandler dbHandler;
+ private final A1cCalculatorActivity activity;
+
+ public A1CCalculatorPresenter(@NonNull final A1cCalculatorActivity activity,
+ @NonNull final DatabaseHandler dbHandler) {
+ this.activity = activity;
+ this.dbHandler = dbHandler;
+ }
+
+ public double calculateA1C(String glucose) {
+ if (isInvalidDouble(glucose)) {
+ return 0;
+ }
+
+ double convertedA1C;
+ User user = dbHandler.getUser(1);
+
+ double glucoseDouble = ReadingTools.safeParseDouble(glucose);
+ if (Constants.Units.MG_DL.equals(user.getPreferred_unit())) {
+ convertedA1C = GlucosioConverter.glucoseToA1C(glucoseDouble);
+ } else {
+ convertedA1C = GlucosioConverter.glucoseToA1C(GlucosioConverter.glucoseToMgDl(glucoseDouble));
+ }
+ if ("percentage".equals(user.getPreferred_unit_a1c())) {
+ return convertedA1C;
+ } else {
+ return GlucosioConverter.a1cNgspToIfcc(convertedA1C);
+ }
+ }
+
+ private boolean isInvalidDouble(String value) {
+ return value == null || value.length() == 0 || (value.length() == 1 && !Character.isDigit(value.charAt(0)));
+ }
+
+ public void checkGlucoseUnit() {
+ if (!Constants.Units.MG_DL.equals(dbHandler.getUser(1).getPreferred_unit())) {
+ activity.setMmol();
+ }
+ }
+
+ public void saveA1C(double a1c) {
+ User user = dbHandler.getUser(1);
+ double finalA1c = a1c;
+ if (!"percentage".equals(user.getPreferred_unit_a1c())) {
+ finalA1c = GlucosioConverter.a1cIfccToNgsp(a1c);
+ }
+
+ HB1ACReading a1cReading = new HB1ACReading(finalA1c, new Date());
+ dbHandler.addHB1ACReading(a1cReading);
+ activity.finish();
+ }
+
+ public String getA1cUnit() {
+ return dbHandler.getUser(1).getPreferred_unit_a1c();
+ }
+
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddA1CPresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddA1CPresenter.java
new file mode 100644
index 0000000..78a662e
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddA1CPresenter.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.activity.AddA1CActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.HB1ACReading;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class AddA1CPresenter extends AddReadingPresenter {
+ private DatabaseHandler dB;
+ private AddA1CActivity activity;
+
+ public AddA1CPresenter(AddA1CActivity addA1CActivity) {
+ this.activity = addA1CActivity;
+ dB = new DatabaseHandler(addA1CActivity.getApplicationContext());
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading) {
+ if (validateDate(date) && validateTime(time) && validateA1C(reading)) {
+
+ HB1ACReading hReading = generateHB1ACReading(reading);
+ dB.addHB1ACReading(hReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading, long oldId) {
+ if (validateDate(date) && validateTime(time) && validateText(reading)) {
+
+ HB1ACReading hReading = generateHB1ACReading(reading);
+ dB.editHB1ACReading(oldId, hReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ private HB1ACReading generateHB1ACReading(String reading) {
+ Date finalDateTime = getReadingTime();
+
+ double finalReading;
+ if ("percentage".equals(getA1CUnitMeasuerement())) {
+ finalReading = ReadingTools.safeParseDouble(reading);
+ } else {
+ finalReading = GlucosioConverter.a1cIfccToNgsp(ReadingTools.safeParseDouble(reading));
+ }
+
+ return new HB1ACReading(finalReading, finalDateTime);
+ }
+
+ public String getA1CUnitMeasuerement() {
+ return dB.getUser(1).getPreferred_unit_a1c();
+ }
+
+ public HB1ACReading getHB1ACReadingById(Long id) {
+ return dB.getHB1ACReadingById(id);
+ }
+
+ // Validator
+ private boolean validateA1C(String reading) {
+ return validateText(reading);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddCholesterolPresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddCholesterolPresenter.java
new file mode 100644
index 0000000..9532cab
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddCholesterolPresenter.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.activity.AddCholesterolActivity;
+import org.glucosio.android.db.CholesterolReading;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class AddCholesterolPresenter extends AddReadingPresenter {
+ private DatabaseHandler dB;
+ private AddCholesterolActivity activity;
+
+ public AddCholesterolPresenter(AddCholesterolActivity addCholesterolActivity) {
+ this.activity = addCholesterolActivity;
+ dB = new DatabaseHandler(addCholesterolActivity.getApplicationContext());
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String totalCho, String LDLCho, String HDLCho) {
+ if (validateDate(date) && validateTime(time) && validateCholesterol(totalCho) && validateCholesterol(LDLCho) && validateCholesterol(HDLCho)) {
+ CholesterolReading cReading = generateCholesterolReading(totalCho, LDLCho, HDLCho);
+ dB.addCholesterolReading(cReading);
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String totalCho, String LDLCho, String HDLCho, long oldId) {
+ if (validateDate(date) && validateTime(time) && validateCholesterol(totalCho) && validateCholesterol(LDLCho) && validateCholesterol(HDLCho)) {
+ CholesterolReading cReading = generateCholesterolReading(totalCho, LDLCho, HDLCho);
+ dB.editCholesterolReading(oldId, cReading);
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+
+ }
+ }
+
+ private CholesterolReading generateCholesterolReading(String totalCho, String LDLCho, String HDLCho) {
+ Date finalDateTime = getReadingTime();
+ double totalChoFinal = ReadingTools.safeParseDouble(totalCho);
+ double LDLChoFinal = ReadingTools.safeParseDouble(LDLCho);
+ double HDLChoFinal = ReadingTools.safeParseDouble(HDLCho);
+ return new CholesterolReading(totalChoFinal, LDLChoFinal, HDLChoFinal, finalDateTime);
+ }
+
+ public String getUnitMeasurement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public CholesterolReading getCholesterolReadingById(Long id) {
+ return dB.getCholesterolReading(id);
+ }
+
+ // Validator
+ private boolean validateCholesterol(String reading) {
+ return validateText(reading);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddGlucosePresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddGlucosePresenter.java
new file mode 100644
index 0000000..6d8e1e1
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddGlucosePresenter.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+import org.glucosio.android.Constants;
+import org.glucosio.android.activity.AddGlucoseActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.GlucoseReading;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.ReadingTools;
+import org.glucosio.android.tools.SplitDateTime;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+public class AddGlucosePresenter extends AddReadingPresenter {
+ private static final int UNKNOWN_ID = -1;
+ private DatabaseHandler dB;
+ private AddGlucoseActivity activity;
+ private ReadingTools rTools;
+
+ public AddGlucosePresenter(AddGlucoseActivity addGlucoseActivity) {
+ this.activity = addGlucoseActivity;
+ dB = new DatabaseHandler(addGlucoseActivity.getApplicationContext());
+ rTools = new ReadingTools();
+ }
+
+ public void updateSpinnerTypeTime() {
+ setReadingTimeNow();
+ activity.updateSpinnerTypeTime(timeToSpinnerType());
+ }
+
+ private int timeToSpinnerType() {
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ Date formatted = Calendar.getInstance().getTime();
+
+ SplitDateTime addSplitDateTime = new SplitDateTime(formatted, inputFormat);
+ int hour = Integer.parseInt(addSplitDateTime.getHour());
+
+ return hourToSpinnerType(hour);
+ }
+
+ public int hourToSpinnerType(int hour) {
+ return rTools.hourToSpinnerType(hour);
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading, String type, String notes) {
+ dialogOnAddButtonPressed(time, date, reading, type, notes, UNKNOWN_ID);
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading, String type, String notes, long oldId) {
+ if (validateDate(date) && validateTime(time) && validateGlucose(reading) && validateType(type)) {
+ Date finalDateTime = getReadingTime();
+ Number number = ReadingTools.parseReading(reading);
+ if (number == null) {
+ activity.showErrorMessage();
+ } else {
+ boolean isReadingAdded = createReading(type, notes, oldId, finalDateTime, number);
+ if (!isReadingAdded) {
+ activity.showDuplicateErrorMessage();
+ } else {
+ activity.finishActivity();
+ }
+ }
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ private boolean createReading(String type, String notes, long oldId, Date finalDateTime, Number number) {
+ boolean isReadingAdded;
+ double readingValue;
+ if (Constants.Units.MG_DL.equals(getUnitMeasurement())) {
+ readingValue = number.doubleValue();
+ } else {
+ readingValue = GlucosioConverter.glucoseToMgDl(number.doubleValue());
+ }
+ GlucoseReading gReading = new GlucoseReading(readingValue, type, finalDateTime, notes);
+ if (oldId == UNKNOWN_ID) {
+ isReadingAdded = dB.addGlucoseReading(gReading);
+ } else {
+ isReadingAdded = dB.editGlucoseReading(oldId, gReading);
+ }
+ return isReadingAdded;
+ }
+
+ public Integer retrieveSpinnerID(String measuredTypeText, List measuredTypelist) {
+ int measuredId = 0;
+ boolean isFound = false;
+ for (String measuredType : measuredTypelist) {
+ if (measuredType.equals(measuredTypeText)) {
+ isFound = true;
+ break;
+ }
+ measuredId++;
+ }
+ // if type is not found, it's return null
+ return isFound ? measuredId : null;
+ }
+
+ public String getUnitMeasurement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public GlucoseReading getGlucoseReadingById(Long id) {
+ return dB.getGlucoseReadingById(id);
+ }
+
+ // Validator
+ private boolean validateGlucose(String reading) {
+ if (validateText(reading)) {
+ if (Constants.Units.MG_DL.equals(getUnitMeasurement())) {
+ // We store data in db in mg/dl
+ Double readingValue = ReadingTools.safeParseDouble(reading);
+ //TODO: Add custom ranges
+ return readingValue > 19 && readingValue < 601;
+ } else if (Constants.Units.MMOL_L.equals(getUnitMeasurement())) {
+ // Convert mmol/L Unit
+ Double readingValue = ReadingTools.safeParseDouble(reading);
+ return readingValue > 1.0545 && readingValue < 33.3555;
+ } else {
+ // IT return always true: we don't have ranges yet.
+ return true;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ public boolean isFreeStyleLibreEnabled() {
+ SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
+ return sharedPref.getBoolean("pref_freestyle_libre", false);
+ }
+
+ private boolean validateType(String type) {
+ return validateText(type);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddKetonePresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddKetonePresenter.java
new file mode 100644
index 0000000..02efc2c
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddKetonePresenter.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.activity.AddKetoneActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.KetoneReading;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class AddKetonePresenter extends AddReadingPresenter {
+ private DatabaseHandler dB;
+ private AddKetoneActivity activity;
+
+ public AddKetonePresenter(AddKetoneActivity addKetoneActivity) {
+ this.activity = addKetoneActivity;
+ dB = new DatabaseHandler(addKetoneActivity.getApplicationContext());
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading) {
+ if (validateDate(date) && validateTime(time) && validateKetone(reading)) {
+
+ KetoneReading kReading = generateKetoneReading(reading);
+ dB.addKetoneReading(kReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading, long oldId) {
+ if (validateDate(date) && validateTime(time) && validateText(reading)) {
+
+ KetoneReading kReading = generateKetoneReading(reading);
+ dB.editKetoneReading(oldId, kReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ private KetoneReading generateKetoneReading(String reading) {
+ Date finalDateTime = getReadingTime();
+ double finalReading = ReadingTools.safeParseDouble(reading);
+ return new KetoneReading(finalReading, finalDateTime);
+ }
+
+ public String getUnitMeasuerement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public KetoneReading getKetoneReadingById(Long id) {
+ return dB.getKetoneReadingById(id);
+ }
+
+ // Validator
+ private boolean validateKetone(String reading) {
+ return validateText(reading);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddPressurePresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddPressurePresenter.java
new file mode 100644
index 0000000..2432c68
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddPressurePresenter.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.activity.AddPressureActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.PressureReading;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class AddPressurePresenter extends AddReadingPresenter {
+ private DatabaseHandler dB;
+ private AddPressureActivity activity;
+
+
+ public AddPressurePresenter(AddPressureActivity addPressureActivity) {
+ this.activity = addPressureActivity;
+ dB = new DatabaseHandler(addPressureActivity.getApplicationContext());
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String minReading, String maxReading) {
+ if (validateDate(date) && validateTime(time) && validatePressure(minReading) && validatePressure(maxReading)) {
+ PressureReading pReading = generatePressureReading(minReading, maxReading);
+ dB.addPressureReading(pReading);
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String minReading, String maxReading, long oldId) {
+ if (validateDate(date) && validateTime(time) && validatePressure(minReading) && validatePressure(maxReading)) {
+ PressureReading pReading = generatePressureReading(minReading, maxReading);
+ dB.editPressureReading(oldId, pReading);
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ private PressureReading generatePressureReading(String minReading, String maxReading) {
+ Date finalDateTime = getReadingTime();
+ double minFinalReading = ReadingTools.safeParseDouble(minReading);
+ double maxFinalReading = ReadingTools.safeParseDouble(maxReading);
+ return new PressureReading(minFinalReading, maxFinalReading, finalDateTime);
+ }
+
+ // Getters and Setters
+
+ public String getUnitMeasuerement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public PressureReading getPressureReadingById(long editId) {
+ return dB.getPressureReading(editId);
+ }
+
+ // Validator
+ private boolean validatePressure(String reading) {
+ return validateText(reading);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddReadingPresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddReadingPresenter.java
new file mode 100644
index 0000000..4d6780b
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddReadingPresenter.java
@@ -0,0 +1,87 @@
+package org.glucosio.android.presenter;
+
+import android.text.TextUtils;
+
+import org.glucosio.android.tools.SplitDateTime;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+public class AddReadingPresenter {
+
+ private String readingYear;
+ private String readingMonth;
+ private String readingDay;
+ private String readingHour;
+ private String readingMinute;
+
+ public void updateReadingSplitDateTime(Date readingDate) {
+ DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ SplitDateTime splitDateTime = new SplitDateTime(readingDate, inputFormat);
+ this.readingDay = splitDateTime.getDay();
+ this.readingHour = splitDateTime.getHour();
+ this.readingMinute = splitDateTime.getMinute();
+ this.readingYear = splitDateTime.getYear();
+ this.readingMonth = splitDateTime.getMonth();
+ }
+
+ public void setReadingTimeNow() {
+ Date formatted = new Date();
+ updateReadingSplitDateTime(formatted);
+ }
+
+ public Calendar getReadingCal() {
+ Calendar cal = Calendar.getInstance();
+ cal.set(Integer.parseInt(readingYear), Integer.parseInt(readingMonth) - 1, Integer.parseInt(readingDay), Integer.parseInt(readingHour), Integer.parseInt(readingMinute));
+ return cal;
+ }
+
+ public Date getReadingTime() {
+ return getReadingCal().getTime();
+ }
+
+ public String getReadingYear() {
+ return readingYear;
+ }
+
+ public void setReadingYear(String readingYear) {
+ this.readingYear = readingYear;
+ }
+
+ public String getReadingMonth() {
+ return readingMonth;
+ }
+
+ public void setReadingMonth(String readingMonth) {
+ this.readingMonth = readingMonth;
+ }
+
+ public void setReadingDay(String readingDay) {
+ this.readingDay = readingDay;
+ }
+
+ public void setReadingHour(String readingHour) {
+ this.readingHour = readingHour;
+ }
+
+ public void setReadingMinute(String readingMinute) {
+ this.readingMinute = readingMinute;
+ }
+
+ protected boolean validateText(String text) {
+ return !TextUtils.isEmpty(text);
+ }
+
+ // Validator
+ protected boolean validateTime(String time) {
+ //TODO check if it can be empty or not valid in other way in different sdk
+ return !TextUtils.isEmpty(time);
+ }
+
+ protected boolean validateDate(String date) {
+ //TODO check if it can be empty or not valid in other way in different sdk
+ return !TextUtils.isEmpty(date);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AddWeightPresenter.java b/app/src/main/java/org/glucosio/android/presenter/AddWeightPresenter.java
new file mode 100644
index 0000000..c3b02dd
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AddWeightPresenter.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.activity.AddWeightActivity;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.WeightReading;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.ReadingTools;
+
+import java.util.Date;
+
+public class AddWeightPresenter extends AddReadingPresenter {
+ private DatabaseHandler dB;
+ private AddWeightActivity activity;
+
+
+ public AddWeightPresenter(AddWeightActivity addWeightActivity) {
+ this.activity = addWeightActivity;
+ dB = new DatabaseHandler(addWeightActivity.getApplicationContext());
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading) {
+ if (validateDate(date) && validateTime(time) && validateWeight(reading)) {
+
+ WeightReading wReading = generateWeightReading(reading);
+ dB.addWeightReading(wReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ public void dialogOnAddButtonPressed(String time, String date, String reading, long oldId) {
+ if (validateDate(date) && validateTime(time) && validateWeight(reading)) {
+
+ WeightReading wReading = generateWeightReading(reading);
+ dB.editWeightReading(oldId, wReading);
+
+ activity.finishActivity();
+ } else {
+ activity.showErrorMessage();
+ }
+ }
+
+ private WeightReading generateWeightReading(String reading) {
+ Date finalDateTime = getReadingTime();
+
+ double finalReading;
+
+ if ("kilograms".equals(getWeightUnitMeasuerement())) {
+ finalReading = ReadingTools.safeParseDouble(reading);
+ } else {
+ finalReading = GlucosioConverter.lbToKg(ReadingTools.safeParseDouble(reading));
+ }
+
+ return new WeightReading(finalReading, finalDateTime);
+ }
+
+ // Getters and Setters
+
+ public String getWeightUnitMeasuerement() {
+ return dB.getUser(1).getPreferred_unit_weight();
+ }
+
+ public WeightReading getWeightReadingById(Long id) {
+ return dB.getWeightReadingById(id);
+ }
+
+ // Validator
+ private boolean validateWeight(String reading) {
+ return validateText(reading);
+ }
+
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/AssistantPresenter.java b/app/src/main/java/org/glucosio/android/presenter/AssistantPresenter.java
new file mode 100644
index 0000000..e464467
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/AssistantPresenter.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.fragment.AssistantFragment;
+
+public class AssistantPresenter {
+ private AssistantFragment fragment;
+
+ public AssistantPresenter(AssistantFragment assistantFragment) {
+ this.fragment = assistantFragment;
+ }
+
+ public void userAskedAddReading() {
+ fragment.addReading();
+ }
+
+ public void userAskedExport() {
+ fragment.startExportActivity();
+ }
+
+ public void userAskedA1CCalculator() {
+ fragment.startA1CCalculatorActivity();
+ }
+
+ public void userSupportAsked() {
+ fragment.openSupportDialog();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/ExportPresenter.java b/app/src/main/java/org/glucosio/android/presenter/ExportPresenter.java
new file mode 100644
index 0000000..cda36fa
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/ExportPresenter.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.content.Context;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.os.Environment;
+import android.support.annotation.Nullable;
+import android.support.v4.content.FileProvider;
+import android.util.Log;
+
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.GlucoseReading;
+import org.glucosio.android.tools.ReadingToCSV;
+import org.glucosio.android.view.ExportView;
+import org.joda.time.DateTime;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.util.List;
+
+import io.realm.Realm;
+
+public class ExportPresenter {
+ private static final String EMPTY = "Empty Readings while exporting";
+ private final ExportView view;
+ private final Context context;
+ private final DatabaseHandler databaseHandler;
+ private DateTime fromDate;
+ private DateTime toDate;
+
+ public ExportPresenter(ExportView exportView, Context context, DatabaseHandler databaseHandler) {
+ view = exportView;
+ this.context = context;
+ this.databaseHandler = databaseHandler;
+ }
+
+ public void onExportClicked(final boolean isExportAll) {
+
+ final String preferredUnit = databaseHandler.getUser(1).getPreferred_unit();
+
+ new AsyncTask() {
+ @Override
+ protected String doInBackground(Void... params) {
+
+ Realm realm = databaseHandler.getNewRealmInstance();
+ final List readings;
+ if (isExportAll) {
+ readings = databaseHandler.getGlucoseReadings(realm);
+ } else {
+ readings = databaseHandler.getGlucoseReadings(realm, fromDate.toDate(), toDate.toDate());
+ }
+
+ if (readings.isEmpty()) {
+ realm.close();
+ return EMPTY;
+ }
+
+ if (dirExistsAndCanWrite()) {
+ Log.i("glucosio", "Dir exists");
+ String csvFile = exportReadings(readings, preferredUnit);
+ realm.close();
+ return csvFile;
+ } else {
+ Log.i("glucosio", "Dir NOT exists");
+ realm.close();
+ return null;
+ }
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... values) {
+ super.onProgressUpdate(values);
+ view.onExportStarted(values[0]);
+ }
+
+ @Override
+ protected void onPostExecute(String filename) {
+ super.onPostExecute(filename);
+ if (EMPTY.equals(filename)) {
+ view.onNoItemsToExport();
+ } else if (filename == null) {
+ view.onExportError();
+ } else {
+ Context applicationContext = context.getApplicationContext();
+ Uri uri = FileProvider.getUriForFile(applicationContext,
+ applicationContext.getPackageName() + ".provider.fileprovider", new File(filename));
+ view.onExportFinish(uri);
+ }
+ }
+ }.execute();
+ }
+
+ @Nullable
+ private String exportReadings(List readings, String preferredUnit) {
+ File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/glucosio", "glucosio_export_" + System.currentTimeMillis() / 1000 + ".csv");
+
+ FileOutputStream fileOutputStream = null;
+ OutputStreamWriter osw = null;
+
+ try {
+ fileOutputStream = new FileOutputStream(file);
+ osw = new OutputStreamWriter(fileOutputStream);
+
+ new ReadingToCSV(context, preferredUnit).createCSVFile(readings, osw);
+
+ return file.getPath();
+ } catch (IOException e) {
+ Log.e("ExportPresenter", "Exporting CSV", e);
+ return null;
+ } finally {
+ closeSafe(osw);
+ closeSafe(fileOutputStream);
+ }
+ }
+
+ private void closeSafe(@Nullable Closeable closeable) {
+ if (closeable == null) return;
+ try {
+ closeable.close();
+ } catch (IOException ignored) {
+ //ignored
+ }
+ }
+
+ private boolean dirExistsAndCanWrite() {
+ File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/glucosio");
+ return (file.exists() || file.mkdirs()) && file.canWrite();
+ }
+
+ public void setTo(int year, int monthOfYear, int dayOfMonth) {
+ toDate = new DateTime(year, monthOfYear, dayOfMonth, 0, 0);
+ }
+
+ public void setFrom(int year, int monthOfYear, int dayOfMonth) {
+ fromDate = new DateTime(year, monthOfYear, dayOfMonth, 0, 0);
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/ExternalViewPresenter.java b/app/src/main/java/org/glucosio/android/presenter/ExternalViewPresenter.java
new file mode 100644
index 0000000..74fc767
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/ExternalViewPresenter.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.text.TextUtils;
+
+import org.glucosio.android.tools.network.NetworkConnectivity;
+
+public class ExternalViewPresenter {
+
+ private ExternalViewPresenter.View view;
+ private NetworkConnectivity network;
+
+ public ExternalViewPresenter(View view, NetworkConnectivity network) {
+ this.view = view;
+ this.network = network;
+ }
+
+ public void onViewCreated() {
+ if (network.isConnected()) {
+ String title = view.extractTitle();
+ String url = view.extractUrl();
+ parametersPrecondition(title, url);
+ view.setupToolbarTitle(title);
+ view.loadExternalUrl(url);
+ } else {
+ view.showNoConnectionWarning();
+ }
+ }
+
+ private void parametersPrecondition(String title, String url) {
+ if ((invalidParam(title)) || invalidParam(url)) {
+ throw new IllegalArgumentException("Invalid arguments: need URL and TITLE");
+ }
+ }
+
+ private boolean invalidParam(String url) {
+ return TextUtils.isEmpty(url);
+ }
+
+ public interface View {
+ void setupToolbarTitle(String link);
+
+ String extractTitle();
+
+ String extractUrl();
+
+ void loadExternalUrl(String url);
+
+ void showNoConnectionWarning();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/HelloPresenter.java b/app/src/main/java/org/glucosio/android/presenter/HelloPresenter.java
new file mode 100644
index 0000000..bde58ab
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/HelloPresenter.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.text.TextUtils;
+
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.User;
+import org.glucosio.android.db.UserBuilder;
+import org.glucosio.android.view.HelloView;
+
+
+public class HelloPresenter {
+ private final DatabaseHandler dB;
+ private final HelloView helloView;
+
+ private int id;
+ private String name;
+
+ public HelloPresenter(final HelloView helloView, final DatabaseHandler dbHandler) {
+ this.helloView = helloView;
+ dB = dbHandler;
+ }
+
+ public void loadDatabase() {
+ id = 1; // Id is always 1. We don't support multi-user (for now :D).
+ name = "Test Account"; //TODO: add input for name in Tips;
+ }
+
+ public void onNextClicked(String age, String gender, String language, String country, int type, String unit) {
+ if (validateAge(age)) {
+ saveToDatabase(id, name, language, country, Integer.parseInt(age), gender, type, unit);
+ helloView.startMainView();
+ } else {
+ helloView.displayErrorWrongAge();
+ }
+ }
+
+ private boolean validateAge(String age) {
+ if (TextUtils.isEmpty(age)) {
+ return false;
+ } else if (!TextUtils.isDigitsOnly(age)) {
+ return false;
+ } else {
+ int finalAge = Integer.parseInt(age);
+ return finalAge > 0 && finalAge < 120;
+ }
+ }
+
+ private void saveToDatabase(final int id, final String name, final String language,
+ final String country, final int age, final String gender,
+ final int diabetesType, final String unitMeasurement) {
+ User user = new UserBuilder()
+ .setId(id)
+ .setName(name)
+ .setPreferredLanguage(language)
+ .setCountry(country)
+ .setAge(age)
+ .setGender(gender)
+ .setDiabetesType(diabetesType)
+ .setPreferredUnit(unitMeasurement)
+ .setPreferredA1CUnit("percentage")
+ .setPreferredWeightUnit("kilograms")
+ .setPreferredRange("ADA")
+ .setMinRange(70)
+ .setMaxRange(180)
+ .createUser();
+ dB.addUser(user); // We use ADA range by default
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/HistoryPresenter.java b/app/src/main/java/org/glucosio/android/presenter/HistoryPresenter.java
new file mode 100644
index 0000000..bf8cb06
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/HistoryPresenter.java
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.fragment.HistoryFragment;
+
+import java.util.List;
+
+public class HistoryPresenter {
+
+ private DatabaseHandler dB;
+ private HistoryFragment fragment;
+
+ public HistoryPresenter(HistoryFragment historyFragment) {
+ this.fragment = historyFragment;
+ dB = new DatabaseHandler(historyFragment.getContext());
+ }
+
+ public boolean isdbEmpty() {
+ return dB.getGlucoseReadings().size() == 0;
+ }
+
+
+ public String convertDate(String date) {
+ return fragment.convertDate(date);
+ }
+
+ public void onDeleteClicked(long idToDelete, int metricID) {
+ switch (metricID) {
+ // Glucose
+ case 0:
+ dB.deleteGlucoseReading(dB.getGlucoseReadingById(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ // HB1AC
+ case 1:
+ dB.deleteHB1ACReading(dB.getHB1ACReadingById(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ // Cholesterol
+ case 2:
+ dB.deleteCholesterolReading(dB.getCholesterolReading(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ // Pressure
+ case 3:
+ dB.deletePressureReading(dB.getPressureReading(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ //Ketones
+ case 4:
+ dB.deleteKetoneReading(dB.getKetoneReadingById(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ // Weight
+ case 5:
+ dB.deleteWeightReading(dB.getWeightReadingById(idToDelete));
+ fragment.reloadFragmentAdapter();
+ break;
+ default:
+ break;
+ }
+ fragment.notifyAdapter();
+ fragment.updateToolbarBehaviour();
+ }
+
+ // Getters
+ public String getUnitMeasuerement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public String getWeightUnitMeasurement() {
+ return dB.getUser(1).getPreferred_unit_weight();
+ }
+
+ public String getA1cUnitMeasurement() {
+ return dB.getUser(1).getPreferred_unit_a1c();
+ }
+
+ public List getGlucoseId() {
+ return dB.getGlucoseIdAsList();
+ }
+
+ public List getGlucoseReadingType() {
+ return dB.getGlucoseTypeAsList();
+ }
+
+ public List getGlucoseNotes() {
+ return dB.getGlucoseNotesAsList();
+ }
+
+ public List getGlucoseReading() {
+ return dB.getGlucoseReadingAsList();
+ }
+
+ public List getGlucoseDateTime() {
+ return dB.getGlucoseDateTimeAsList();
+ }
+
+ public int getGlucoseReadingsNumber() {
+ return dB.getGlucoseReadingAsList().size();
+ }
+
+ public List getKetoneReading() {
+ return dB.getKetoneReadingAsArray();
+ }
+
+ public List getKetoneDateTime() {
+ return dB.getKetoneDateTimeAsArray();
+ }
+
+ public List getKetoneId() {
+ return dB.getKetoneIdAsArray();
+ }
+
+ public int getKetoneReadingsNumber() {
+ return dB.getKetoneDateTimeAsArray().size();
+ }
+
+ public List getCholesterolId() {
+ return dB.getCholesterolIdAsArray();
+ }
+
+ public List getCholesterolDateTime() {
+ return dB.getCholesterolDateTimeAsArray();
+ }
+
+ public List getHDLCholesterolReading() {
+ return dB.getHDLCholesterolReadingAsArray();
+ }
+
+ public List getLDLCholesterolReading() {
+ return dB.getLDLCholesterolReadingAsArray();
+ }
+
+ public List getTotalCholesterolReading() {
+ return dB.getTotalCholesterolReadingAsArray();
+ }
+
+ public int getCholesterolReadingsNumber() {
+ return dB.getCholesterolIdAsArray().size();
+ }
+
+ public List getHB1ACId() {
+ return dB.getHB1ACIdAsArray();
+ }
+
+ public List getHB1ACDateTime() {
+ return dB.getHB1ACDateTimeAsArray();
+ }
+
+ public List getHB1ACReading() {
+ return dB.getHB1ACReadingAsArray();
+ }
+
+ public int getHB1ACReadingsNumber() {
+ return dB.getHB1ACReadingAsArray().size();
+ }
+
+ public int getPressureReadings() {
+ return dB.getPressureReadings().size();
+ }
+
+ public List getPressureDateTime() {
+ return dB.getPressureDateTimeAsArray();
+ }
+
+ public List getPressureId() {
+ return dB.getPressureIdAsArray();
+ }
+
+ public List getMinPressureReading() {
+ return dB.getMinPressureReadingAsArray();
+ }
+
+ public List getMaxPressureReading() {
+ return dB.getMaxPressureReadingAsArray();
+ }
+
+ public int getPressureReadingsNumber() {
+ return dB.getPressureIdAsArray().size();
+ }
+
+ public List getWeightReadings() {
+ return dB.getWeightReadingAsArray();
+ }
+
+ public List getWeightDateTime() {
+ return dB.getWeightReadingDateTimeAsArray();
+ }
+
+ public List getWeightId() {
+ return dB.getWeightIdAsArray();
+ }
+
+ public int getWeightReadingsNumber() {
+ return dB.getWeightIdAsArray().size();
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/MainPresenter.java b/app/src/main/java/org/glucosio/android/presenter/MainPresenter.java
new file mode 100644
index 0000000..fb8d4cf
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/MainPresenter.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import android.util.Log;
+
+import org.glucosio.android.activity.MainActivity;
+import org.glucosio.android.db.DatabaseHandler;
+
+public class MainPresenter {
+
+ private DatabaseHandler dB;
+
+ public MainPresenter(MainActivity mainActivity, DatabaseHandler databaseHandler) {
+ dB = databaseHandler;
+ Log.i("msg::", "initiated dB object");
+ if (dB.getUser(1) == null) {
+ // if user doesn't exists start hello activity
+ mainActivity.startHelloActivity();
+ } else {
+ // If user already exists, update user's preferred language and recreate MainActivity
+ mainActivity.getLocaleHelper().updateLanguage(mainActivity,
+ dB.getUser(1).getPreferred_language());
+ }
+ }
+
+ public boolean isdbEmpty() {
+ return dB.getGlucoseReadings().size() == 0;
+ }
+}
diff --git a/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java b/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java
new file mode 100644
index 0000000..11085b3
--- /dev/null
+++ b/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java
@@ -0,0 +1,296 @@
+/*
+ * Copyright (C) 2016 Glucosio Foundation
+ *
+ * This file is part of Glucosio.
+ *
+ * Glucosio is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * Glucosio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Glucosio. If not, see .
+ *
+ *
+ */
+
+package org.glucosio.android.presenter;
+
+import org.glucosio.android.R;
+import org.glucosio.android.db.DatabaseHandler;
+import org.glucosio.android.db.GlucoseReading;
+import org.glucosio.android.object.A1cEstimate;
+import org.glucosio.android.object.DoubleGraphObject;
+import org.glucosio.android.tools.GlucosioConverter;
+import org.glucosio.android.tools.TipsManager;
+import org.glucosio.android.view.OverviewView;
+import org.joda.time.DateTime;
+import org.joda.time.Days;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Random;
+
+public class OverviewPresenter {
+
+ private final DatabaseHandler dB;
+ private final OverviewView view;
+
+ private List glucoseReadingsWeek;
+ private List glucoseReadingsMonth;
+ private List glucoseDatetimeWeek;
+ private List glucoseDatetimeMonth;
+ private List glucoseReadings;
+ private List glucoseGraphObjects;
+ private double glucoseMinValue = 0;
+ private double glucoseMaxValue = 0;
+
+ public OverviewPresenter(OverviewView view, DatabaseHandler dB) {
+ this.dB = dB;
+ this.view = view;
+ }
+
+ public boolean isdbEmpty() {
+ return dB.getGlucoseReadings().size() == 0;
+ }
+
+ public void loadDatabase(boolean isNewGraphEnabled) {
+ this.glucoseReadings = dB.getGlucoseReadings();
+ this.glucoseGraphObjects = generateGlucoseGraphPoints(isNewGraphEnabled);
+ this.glucoseReadingsMonth = dB.getAverageGlucoseReadingsByMonth();
+ this.glucoseReadingsWeek = dB.getAverageGlucoseReadingsByWeek();
+ this.glucoseDatetimeWeek = dB.getGlucoseDatetimesByWeek();
+ this.glucoseDatetimeMonth = dB.getGlucoseDatetimesByMonth();
+ this.glucoseMaxValue = dB.getUser(1).getCustom_range_max();
+ this.glucoseMinValue = dB.getUser(1).getCustom_range_min();
+ }
+
+ public String convertDate(String date) {
+ return view.convertDate(date);
+ }
+
+ public String getHB1AC() {
+ // Check if last month is available first
+ if (getGlucoseReadingsMonth().size() > 1) {
+ if ("percentage".equals(dB.getUser(1).getPreferred_unit_a1c())) {
+ return GlucosioConverter.glucoseToA1C(getGlucoseReadingsMonth().get(getGlucoseReadingsMonth().size() - 2)) + " %";
+ } else {
+ return GlucosioConverter.a1cNgspToIfcc(GlucosioConverter.glucoseToA1C(getGlucoseReadingsMonth().get(getGlucoseReadingsMonth().size() - 2))) + " mmol/mol";
+ }
+ } else {
+ return view.getString(R.string.overview_hb1ac_error_no_data);
+ }
+ }
+
+ public boolean isA1cAvailable(int depth) {
+ return getGlucoseReadingsMonth().size() > depth;
+ }
+
+ public List getA1cEstimateList() {
+ ArrayList a1cEstimateList = new ArrayList<>();
+
+ // We don't take this month because A1C is incomplete
+ for (int i = 0; i < getGlucoseReadingsMonth().size() - 1; i++) {
+ double value = GlucosioConverter.glucoseToA1C(getGlucoseReadingsMonth().get(i));
+ String month = convertDateToMonth(getGlucoseDatetimeMonth().get(i));
+ a1cEstimateList.add(new A1cEstimate(value, month));
+ }
+ Collections.reverse(a1cEstimateList);
+ return a1cEstimateList;
+ }
+
+ public String getA1cMonth() {
+ // Check if last month is available first
+ if (getGlucoseReadingsMonth().size() > 1) {
+ return convertDateToMonth(getGlucoseDatetimeMonth().get(getGlucoseDatetimeMonth().size() - 2)) + "";
+ } else {
+ return " ";
+ }
+ }
+
+ public String getLastReading() {
+ return String.valueOf(NumberFormat.getInstance().format(dB.getLastGlucoseReading().getReading()));
+ }
+
+ public String getLastDateTime() {
+ java.text.DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ return inputFormat.format(dB.getLastGlucoseReading().getCreated());
+
+ }
+
+ public String getRandomTip(TipsManager manager) {
+ ArrayList tips = manager.getTips();
+
+ // Get random tip from array
+ int randomNumber = new Random().nextInt(tips.size());
+ return tips.get(randomNumber);
+ }
+
+ public String getUnitMeasurement() {
+ return dB.getUser(1).getPreferred_unit();
+ }
+
+ public int getUserAge() {
+ return dB.getUser(1).getAge();
+ }
+
+ public List getGlucoseReadings() {
+ ArrayList glucoseReadings = new ArrayList<>(glucoseGraphObjects.size());
+ for (int i = 0; i < glucoseGraphObjects.size(); i++) {
+ glucoseReadings.add(glucoseGraphObjects.get(i).getReading());
+ }
+
+ return glucoseReadings;
+ }
+
+ public List getA1cReadings() {
+ return dB.getHB1ACReadingAsArray();
+ }
+
+ public List getA1cReadingsDateTime() {
+ return dB.getHB1ACDateTimeAsArray();
+ }
+
+ public List getKetonesReadings() {
+ return dB.getKetoneReadingAsArray();
+ }
+
+ public List getKetonesReadingsDateTime() {
+ return dB.getKetoneDateTimeAsArray();
+ }
+
+ public List getWeightReadings() {
+ return dB.getWeightReadingAsArray();
+ }
+
+ public List getWeightReadingsDateTime() {
+ return dB.getWeightReadingDateTimeAsArray();
+ }
+
+ public List getMinPressureReadings() {
+ return dB.getMinPressureReadingAsArray();
+ }
+
+ public List getMaxPressureReadings() {
+ return dB.getMaxPressureReadingAsArray();
+ }
+
+ public List getPressureReadingsDateTime() {
+ return dB.getPressureDateTimeAsArray();
+ }
+
+ public List getCholesterolReadings() {
+ return dB.getTotalCholesterolReadingAsArray();
+ }
+
+ public List getCholesterolReadingsDateTime() {
+ return dB.getCholesterolDateTimeAsArray();
+ }
+
+ private List generateGlucoseGraphPoints(boolean isNewGraphEnabled) {
+ final ArrayList finalGraphObjects = new ArrayList<>();
+ if (isNewGraphEnabled) {
+ DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
+ final List glucoseReadings = dB.getLastMonthGlucoseReadings();
+
+ Collections.sort(glucoseReadings, new Comparator() {
+ public int compare(GlucoseReading o1, GlucoseReading o2) {
+ return o1.getCreated().compareTo(o2.getCreated());
+ }
+ });
+
+ DateTime startDate = glucoseReadings.size() > 0 ?
+ minDateTime : DateTime.now();
+ // Transfer values from database to ArrayList as GlucoseGraphObjects
+ for (int i = 0; i < glucoseReadings.size(); i++) {
+ final GlucoseReading reading = glucoseReadings.get(i);
+ final DateTime createdDate = new DateTime(reading.getCreated());
+ //add zero values between current value and last added value
+ addZeroReadings(finalGraphObjects, startDate, createdDate);
+ //add new value
+ finalGraphObjects.add(
+ new DoubleGraphObject(createdDate, reading.getReading())
+ );
+ //update start date
+ startDate = createdDate;
+ }
+ //add last zeros till now
+ addZeroReadings(finalGraphObjects, startDate, DateTime.now());
+ } else {
+ Collections.sort(glucoseReadings, new Comparator() {
+ public int compare(GlucoseReading o1, GlucoseReading o2) {
+ return o1.getCreated().compareTo(o2.getCreated());
+ }
+ });
+ for (int i = 0; i < glucoseReadings.size(); i++) {
+ GlucoseReading glucoseReading = glucoseReadings.get(i);
+ finalGraphObjects.add(
+ new DoubleGraphObject(new DateTime(glucoseReading.getCreated()), glucoseReading.getReading())
+ );
+ }
+ }
+
+ return finalGraphObjects;
+ }
+
+ private void addZeroReadings(final ArrayList graphObjects,
+ final DateTime firstDate,
+ final DateTime lastDate) {
+ int daysBetween = Days.daysBetween(firstDate, lastDate).getDays();
+ for (int i = 1; i < daysBetween; i++) {
+ graphObjects.add(new DoubleGraphObject(firstDate.plusDays(i), 0));
+ }
+ }
+
+ public ArrayList getGraphGlucoseDateTime() {
+ ArrayList glucoseDatetime = new ArrayList<>();
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
+
+ for (int i = 0; i < glucoseGraphObjects.size(); i++) {
+ glucoseDatetime.add(dateTimeFormatter.print(glucoseGraphObjects.get(i).getCreated()));
+ }
+ return glucoseDatetime;
+ }
+
+ public List