From 7e11ac5d3c345f0f1227a070a1c10eee885bcfce Mon Sep 17 00:00:00 2001 From: Vojtech Bocek Date: Thu, 5 Mar 2015 23:21:49 +0100 Subject: Add UI for entering pattern for device decryption Change-Id: Ia2d3268a96423e9ca3846500c57e674c4f8fa60b Signed-off-by: Vojtech Bocek --- gui/Android.mk | 3 +- gui/devices/1080x1920/res/ui.xml | 8 + gui/devices/1920x1200/res/ui.xml | 8 + gui/devices/320x320/res/ui.xml | 8 + gui/devices/480x800/res/ui.xml | 8 + gui/devices/800x480/res/ui.xml | 8 + gui/devices/landscape/res/landscape.xml | 40 ++++ gui/devices/portrait/res/portrait.xml | 40 ++++ gui/devices/watch/res/watch.xml | 40 ++++ gui/objects.hpp | 52 ++++++ gui/pages.cpp | 7 + gui/patternpassword.cpp | 313 ++++++++++++++++++++++++++++++++ partition.cpp | 1 + variables.h | 1 + 14 files changed, 536 insertions(+), 1 deletion(-) create mode 100644 gui/patternpassword.cpp diff --git a/gui/Android.mk b/gui/Android.mk index afaa5da9c..e5e58658b 100644 --- a/gui/Android.mk +++ b/gui/Android.mk @@ -26,7 +26,8 @@ LOCAL_SRC_FILES := \ blanktimer.cpp \ partitionlist.cpp \ mousecursor.cpp \ - scrolllist.cpp + scrolllist.cpp \ + patternpassword.cpp ifneq ($(TWRP_CUSTOM_KEYBOARD),) LOCAL_SRC_FILES += $(TWRP_CUSTOM_KEYBOARD) diff --git a/gui/devices/1080x1920/res/ui.xml b/gui/devices/1080x1920/res/ui.xml index 4661583ca..3135a03dc 100644 --- a/gui/devices/1080x1920/res/ui.xml +++ b/gui/devices/1080x1920/res/ui.xml @@ -199,6 +199,14 @@ + + + + + + + + diff --git a/gui/devices/1920x1200/res/ui.xml b/gui/devices/1920x1200/res/ui.xml index e8290ef21..986c02657 100644 --- a/gui/devices/1920x1200/res/ui.xml +++ b/gui/devices/1920x1200/res/ui.xml @@ -213,6 +213,14 @@ + + + + + + + + diff --git a/gui/devices/320x320/res/ui.xml b/gui/devices/320x320/res/ui.xml index 317e63283..5495b7717 100644 --- a/gui/devices/320x320/res/ui.xml +++ b/gui/devices/320x320/res/ui.xml @@ -202,6 +202,14 @@ + + + + + + + + diff --git a/gui/devices/480x800/res/ui.xml b/gui/devices/480x800/res/ui.xml index 72183fee3..3c320a7eb 100644 --- a/gui/devices/480x800/res/ui.xml +++ b/gui/devices/480x800/res/ui.xml @@ -194,6 +194,14 @@ + + + + + + + + diff --git a/gui/devices/800x480/res/ui.xml b/gui/devices/800x480/res/ui.xml index f35d1d499..12e0ee702 100644 --- a/gui/devices/800x480/res/ui.xml +++ b/gui/devices/800x480/res/ui.xml @@ -214,6 +214,14 @@ + + + + + + + + diff --git a/gui/devices/landscape/res/landscape.xml b/gui/devices/landscape/res/landscape.xml index 0a907cbfe..432e9fb03 100644 --- a/gui/devices/landscape/res/landscape.xml +++ b/gui/devices/landscape/res/landscape.xml @@ -3241,6 +3241,11 @@ + + + decrypt_pattern + + Please Enter Your Password @@ -3277,6 +3282,41 @@ + + + + + + Please Enter Your Pattern + + + + + + + Pattern Failed, Please Try Again + + + + + + + + trydecrypt + + + + + Cancel + + tw_page_done=1 + main + + + + + + diff --git a/gui/devices/portrait/res/portrait.xml b/gui/devices/portrait/res/portrait.xml index 02e3a5a44..76a6dc754 100644 --- a/gui/devices/portrait/res/portrait.xml +++ b/gui/devices/portrait/res/portrait.xml @@ -3268,6 +3268,11 @@ + + + decrypt_pattern + + Please Enter Your Password @@ -3304,6 +3309,41 @@ + + + + + + Please Enter Your Pattern + + + + + + + Pattern Failed, Please Try Again + + + + + + + + trydecrypt + + + + + Cancel + + tw_page_done=1 + main + + + + + + diff --git a/gui/devices/watch/res/watch.xml b/gui/devices/watch/res/watch.xml index c40e981ff..d6a3fa8a2 100644 --- a/gui/devices/watch/res/watch.xml +++ b/gui/devices/watch/res/watch.xml @@ -3261,6 +3261,11 @@ + + + decrypt_pattern + + Please Enter Your Password @@ -3297,6 +3302,41 @@ + + + + + + Please Enter Your Pattern + + + + + + + Pattern Failed, Please Try Again + + + + + + + + trydecrypt + + + + + Cancel + + tw_page_done=1 + main + + + + + + diff --git a/gui/objects.hpp b/gui/objects.hpp index 261141134..d26b0c2ed 100644 --- a/gui/objects.hpp +++ b/gui/objects.hpp @@ -1058,6 +1058,58 @@ private: bool m_present; }; +class GUIPatternPassword : public GUIObject, public RenderObject, public ActionObject +{ +public: + GUIPatternPassword(xml_node<>* node); + virtual ~GUIPatternPassword(); + +public: + virtual int Render(void); + virtual int Update(void); + virtual int NotifyTouch(TOUCH_STATE state, int x, int y); + virtual int SetRenderPos(int x, int y, int w = 0, int h = 0); + +protected: + void CalculateDotPositions(); + void ResetActiveDots(); + void ConnectDot(int dot_idx); + void ConnectIntermediateDots(int dot_idx); + int InDot(int x, int y); + bool DotUsed(int dot_idx); + static bool IsInRect(int x, int y, int rx, int ry, int rw, int rh); + void PatternDrawn(); + + struct Dot { + int x; + int y; + bool active; + }; + + Dot mDots[9]; + int mConnectedDots[9]; + size_t mConnectedDotsLen; + int mCurLineX; + int mCurLineY; + bool mTrackingTouch; + bool mNeedRender; + + COLOR mDotColor; + COLOR mActiveDotColor; + COLOR mLineColor; + ImageResource *mDotImage; + ImageResource *mActiveDotImage; + gr_surface mDotCircle; + gr_surface mActiveDotCircle; + int mDotRadius; + int mLineWidth; + + std::string mPassVar; + GUIAction *mAction; + int mUpdate; +}; + + // Helper APIs xml_node<>* FindNode(xml_node<>* parent, const char* nodename, int depth = 0); std::string LoadAttrString(xml_node<>* element, const char* attrname, const char* defaultvalue = ""); diff --git a/gui/pages.cpp b/gui/pages.cpp index cd5e340d6..23cfcd822 100644 --- a/gui/pages.cpp +++ b/gui/pages.cpp @@ -445,6 +445,13 @@ bool Page::ProcessNode(xml_node<>* page, std::vector*> *templates /* mRenders.push_back(element); mActions.push_back(element); } + else if (type == "patternpassword") + { + GUIPatternPassword* element = new GUIPatternPassword(child); + mObjects.push_back(element); + mRenders.push_back(element); + mActions.push_back(element); + } else if (type == "template") { if (!templates || !child->first_attribute("name")) diff --git a/gui/patternpassword.cpp b/gui/patternpassword.cpp new file mode 100644 index 000000000..4fc781298 --- /dev/null +++ b/gui/patternpassword.cpp @@ -0,0 +1,313 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern "C" { +#include "../twcommon.h" +#include "../minuitwrp/minui.h" +} + +#include "rapidxml.hpp" +#include "objects.hpp" + +GUIPatternPassword::GUIPatternPassword(xml_node<>* node) + : GUIObject(node) +{ + xml_attribute<>* attr; + xml_node<>* child; + + ResetActiveDots(); + mTrackingTouch = false; + mNeedRender = true; + + ConvertStrToColor("blue", &mDotColor); + ConvertStrToColor("white", &mActiveDotColor); + ConvertStrToColor("blue", &mLineColor); + + mDotImage = mActiveDotImage = NULL; + mDotCircle = mActiveDotCircle = NULL; + mDotRadius = 50; + mLineWidth = 35; + + mAction = NULL; + mUpdate = 0; + + if (!node) + return; + + LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); + + mAction = new GUIAction(node); + + child = FindNode(node, "dot"); + if(child) + { + mDotColor = LoadAttrColor(child, "color", mDotColor); + mActiveDotColor = LoadAttrColor(child, "activecolor", mActiveDotColor); + mDotRadius = LoadAttrIntScaleX(child, "radius", mDotRadius); + + mDotImage = LoadAttrImage(child, "image"); + mActiveDotImage = LoadAttrImage(child, "activeimage"); + } + + child = FindNode(node, "line"); + if(child) + { + mLineColor = LoadAttrColor(child, "color", mLineColor); + mLineWidth = LoadAttrIntScaleX(child, "width", mLineWidth); + } + + child = FindNode(node, "data"); + if(child) + mPassVar = LoadAttrString(child, "name", ""); + + + if(!mDotImage || !mDotImage->GetResource() || !mActiveDotImage || !mActiveDotImage->GetResource()) + { + mDotCircle = gr_render_circle(mDotRadius, mDotColor.red, mDotColor.green, mDotColor.blue, mDotColor.alpha); + mActiveDotCircle = gr_render_circle(mDotRadius/2, mActiveDotColor.red, mActiveDotColor.green, mActiveDotColor.blue, mActiveDotColor.alpha); + } + else + mDotRadius = mDotImage->GetWidth()/2; + + SetRenderPos(mRenderX, mRenderY, mRenderW, mRenderH); +} + +GUIPatternPassword::~GUIPatternPassword() +{ + delete mDotImage; + delete mActiveDotImage; + delete mAction; + + if(mDotCircle) + gr_free_surface(mDotCircle); + + if(mActiveDotCircle) + gr_free_surface(mActiveDotCircle); +} + +void GUIPatternPassword::ResetActiveDots() +{ + mConnectedDotsLen = 0; + mCurLineX = mCurLineY = -1; + for(int i = 0; i < 9; ++i) + mDots[i].active = false; +} + +int GUIPatternPassword::SetRenderPos(int x, int y, int w, int h) +{ + mRenderX = x; + mRenderY = y; + + if (w || h) + { + mRenderW = w; + mRenderH = h; + + mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); + SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); + } + + CalculateDotPositions(); + return 0; +} + +void GUIPatternPassword::CalculateDotPositions(void) +{ + const int step_x = (mRenderW - mDotRadius*2) / 2; + const int step_y = (mRenderH - mDotRadius*2) / 2; + int x = mRenderX; + int y = mRenderY; + + for(int r = 0; r < 3; ++r) + { + for(int c = 0; c < 3; ++c) + { + mDots[3*r+c].x = x; + mDots[3*r+c].y = y; + x += step_x; + } + x = mRenderX; + y += step_y; + } +} + +int GUIPatternPassword::Render(void) +{ + if(!isConditionTrue()) + return 0; + + gr_color(mLineColor.red, mLineColor.green, mLineColor.blue, mLineColor.alpha); + for(size_t i = 1; i < mConnectedDotsLen; ++i) { + const Dot& dp = mDots[mConnectedDots[i-1]]; + const Dot& dc = mDots[mConnectedDots[i]]; + gr_line(dp.x + mDotRadius, dp.y + mDotRadius, dc.x + mDotRadius, dc.y + mDotRadius, mLineWidth); + } + + if(mConnectedDotsLen > 0 && mTrackingTouch) { + const Dot& dc = mDots[mConnectedDots[mConnectedDotsLen-1]]; + gr_line(dc.x + mDotRadius, dc.y + mDotRadius, mCurLineX, mCurLineY, mLineWidth); + } + + for(int i = 0; i < 9; ++i) { + if(mDotCircle) { + gr_blit(mDotCircle, 0, 0, gr_get_width(mDotCircle), gr_get_height(mDotCircle), mDots[i].x, mDots[i].y); + if(mDots[i].active) { + gr_blit(mActiveDotCircle, 0, 0, gr_get_width(mActiveDotCircle), gr_get_height(mActiveDotCircle), mDots[i].x + mDotRadius/2, mDots[i].y + mDotRadius/2); + } + } else { + if(mDots[i].active) { + gr_blit(mActiveDotImage->GetResource(), 0, 0, mActiveDotImage->GetWidth(), mActiveDotImage->GetHeight(), + mDots[i].x + (mDotRadius - mActiveDotImage->GetWidth()/2), mDots[i].y + (mDotRadius - mActiveDotImage->GetHeight()/2)); + } else { + gr_blit(mDotImage->GetResource(), 0, 0, mDotImage->GetWidth(), mDotImage->GetHeight(), mDots[i].x, mDots[i].y); + } + } + } + return 0; +} + +int GUIPatternPassword::Update(void) +{ + if(!isConditionTrue()) + return 0; + + int res = mNeedRender ? 2 : 1; + mNeedRender = false; + return res; +} + +bool GUIPatternPassword::IsInRect(int x, int y, int rx, int ry, int rw, int rh) +{ + return x >= rx && y >= ry && x <= rx+rw && y <= ry+rh; +} + +int GUIPatternPassword::InDot(int x, int y) +{ + for(int i = 0; i < 9; ++i) { + if(IsInRect(x, y, mDots[i].x - mDotRadius*1.5, mDots[i].y - mDotRadius*1.5, mDotRadius*6, mDotRadius*6)) + return i; + } + return -1; +} + +bool GUIPatternPassword::DotUsed(int dot_idx) +{ + for(size_t i = 0; i < mConnectedDotsLen; ++i) { + if(mConnectedDots[i] == dot_idx) + return true; + } + return false; +} + +void GUIPatternPassword::ConnectDot(int dot_idx) +{ + if(mConnectedDotsLen >= 9) + { + LOGERR("mConnectedDots in GUIPatternPassword has overflown!\n"); + return; + } + + mConnectedDots[mConnectedDotsLen++] = dot_idx; + mDots[dot_idx].active = true; +} + +void GUIPatternPassword::ConnectIntermediateDots(int dot_idx) +{ + if(mConnectedDotsLen == 0) + return; + + const int last_dot = mConnectedDots[mConnectedDotsLen-1]; + + // The line is vertical and has crossed a point in the middle + if(dot_idx%3 == last_dot%3 && abs(dot_idx - last_dot) > 3) + ConnectDot(3 + dot_idx%3); + // the line is horizontal and has crossed a point in the middle + else if(dot_idx/3 == last_dot/3 && abs(dot_idx - last_dot) > 1) + ConnectDot((dot_idx/3)*3 + 1); + // the line is diagonal and has crossed the middle point + else if((dot_idx == 0 && last_dot == 8) || (dot_idx == 8 && last_dot == 0) || + (dot_idx == 2 && last_dot == 6) || (dot_idx == 6 && last_dot == 2)) + { + ConnectDot(4); + } +} + +int GUIPatternPassword::NotifyTouch(TOUCH_STATE state, int x, int y) +{ + if(!isConditionTrue()) + return -1; + + switch (state) + { + case TOUCH_START: + { + const int dot_idx = InDot(x, y); + if(dot_idx == -1) + break; + + mTrackingTouch = true; + ResetActiveDots(); + ConnectDot(dot_idx); + DataManager::Vibrate("tw_button_vibrate"); + mCurLineX = x; + mCurLineY = y; + mNeedRender = true; + break; + } + case TOUCH_DRAG: + { + if(!mTrackingTouch) + break; + + const int dot_idx = InDot(x, y); + if(dot_idx != -1 && !DotUsed(dot_idx)) + { + ConnectIntermediateDots(dot_idx); + ConnectDot(dot_idx); + DataManager::Vibrate("tw_button_vibrate"); + } + + mCurLineX = x; + mCurLineY = y; + mNeedRender = true; + break; + } + case TOUCH_RELEASE: + { + if(!mTrackingTouch) + break; + + mNeedRender = true; + mTrackingTouch = false; + PatternDrawn(); + ResetActiveDots(); + break; + } + default: + break; + } + return 0; +} + +void GUIPatternPassword::PatternDrawn() +{ + if(!mPassVar.empty() && mConnectedDotsLen > 0) + { + std::string pass; + for(size_t i = 0; i < mConnectedDotsLen; ++i) + pass += '1' + mConnectedDots[i]; + DataManager::SetValue(mPassVar, pass); + } + + if(mAction) + mAction->doActions(); +} diff --git a/partition.cpp b/partition.cpp index 6a7ad718d..87a605bce 100644 --- a/partition.cpp +++ b/partition.cpp @@ -290,6 +290,7 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { Current_File_System = "emmc"; Setup_Image(Display_Error); DataManager::SetValue(TW_IS_ENCRYPTED, 1); + DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); DataManager::SetValue("tw_crypto_display", ""); } else { diff --git a/variables.h b/variables.h index a52a23361..fe0ccf622 100644 --- a/variables.h +++ b/variables.h @@ -146,6 +146,7 @@ #define TW_DOWNLOAD_MODE "tw_download_mode" #define TW_IS_ENCRYPTED "tw_is_encrypted" #define TW_IS_DECRYPTED "tw_is_decrypted" +#define TW_CRYPTO_PWTYPE "tw_crypto_pwtype" #define TW_HAS_CRYPTO "tw_has_crypto" #define TW_CRYPTO_PASSWORD "tw_crypto_password" #define TW_DATA_BLK_DEVICE "tw_data_blk_device" // Original block device - not decrypted -- cgit v1.2.3