Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <time.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #include <string> |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 30 | #include <sstream> |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 31 | |
| 32 | extern "C" { |
| 33 | #include "../twcommon.h" |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 34 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 35 | #include "../minuitwrp/minui.h" |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 36 | #include "../twrp-functions.hpp" |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 37 | #include "rapidxml.hpp" |
| 38 | #include "objects.hpp" |
| 39 | |
| 40 | GUIPatternPassword::GUIPatternPassword(xml_node<>* node) |
| 41 | : GUIObject(node) |
| 42 | { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 43 | xml_node<>* child; |
| 44 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 45 | // 3x3 is the default. |
| 46 | mGridSize = 3; |
| 47 | mDots = new Dot[mGridSize * mGridSize]; |
| 48 | mConnectedDots = new int[mGridSize * mGridSize]; |
| 49 | |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 50 | ResetActiveDots(); |
| 51 | mTrackingTouch = false; |
| 52 | mNeedRender = true; |
| 53 | |
| 54 | ConvertStrToColor("blue", &mDotColor); |
| 55 | ConvertStrToColor("white", &mActiveDotColor); |
| 56 | ConvertStrToColor("blue", &mLineColor); |
| 57 | |
| 58 | mDotImage = mActiveDotImage = NULL; |
| 59 | mDotCircle = mActiveDotCircle = NULL; |
| 60 | mDotRadius = 50; |
| 61 | mLineWidth = 35; |
| 62 | |
| 63 | mAction = NULL; |
| 64 | mUpdate = 0; |
| 65 | |
| 66 | if (!node) |
| 67 | return; |
| 68 | |
| 69 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
| 70 | |
| 71 | mAction = new GUIAction(node); |
| 72 | |
| 73 | child = FindNode(node, "dot"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 74 | if (child) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 75 | { |
| 76 | mDotColor = LoadAttrColor(child, "color", mDotColor); |
| 77 | mActiveDotColor = LoadAttrColor(child, "activecolor", mActiveDotColor); |
| 78 | mDotRadius = LoadAttrIntScaleX(child, "radius", mDotRadius); |
| 79 | |
| 80 | mDotImage = LoadAttrImage(child, "image"); |
| 81 | mActiveDotImage = LoadAttrImage(child, "activeimage"); |
| 82 | } |
| 83 | |
| 84 | child = FindNode(node, "line"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 85 | if (child) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 86 | { |
| 87 | mLineColor = LoadAttrColor(child, "color", mLineColor); |
| 88 | mLineWidth = LoadAttrIntScaleX(child, "width", mLineWidth); |
| 89 | } |
| 90 | |
| 91 | child = FindNode(node, "data"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 92 | if (child) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 93 | mPassVar = LoadAttrString(child, "name", ""); |
| 94 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 95 | child = FindNode(node, "size"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 96 | if (child) { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 97 | mSizeVar = LoadAttrString(child, "name", ""); |
| 98 | |
| 99 | // Use the configured default, if set. |
| 100 | size_t size = LoadAttrInt(child, "default", mGridSize); |
| 101 | Resize(size); |
| 102 | } |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 103 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 104 | if (!mDotImage || !mDotImage->GetResource() || !mActiveDotImage || !mActiveDotImage->GetResource()) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 105 | { |
| 106 | mDotCircle = gr_render_circle(mDotRadius, mDotColor.red, mDotColor.green, mDotColor.blue, mDotColor.alpha); |
| 107 | mActiveDotCircle = gr_render_circle(mDotRadius/2, mActiveDotColor.red, mActiveDotColor.green, mActiveDotColor.blue, mActiveDotColor.alpha); |
| 108 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 109 | else if (mDotImage && mDotImage->GetResource()) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 110 | mDotRadius = mDotImage->GetWidth()/2; |
| 111 | |
| 112 | SetRenderPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 113 | } |
| 114 | |
| 115 | GUIPatternPassword::~GUIPatternPassword() |
| 116 | { |
| 117 | delete mDotImage; |
| 118 | delete mActiveDotImage; |
| 119 | delete mAction; |
| 120 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 121 | delete[] mDots; |
| 122 | delete[] mConnectedDots; |
| 123 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 124 | if (mDotCircle) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 125 | gr_free_surface(mDotCircle); |
| 126 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 127 | if (mActiveDotCircle) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 128 | gr_free_surface(mActiveDotCircle); |
| 129 | } |
| 130 | |
| 131 | void GUIPatternPassword::ResetActiveDots() |
| 132 | { |
| 133 | mConnectedDotsLen = 0; |
| 134 | mCurLineX = mCurLineY = -1; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 135 | for (size_t i = 0; i < mGridSize * mGridSize; ++i) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 136 | mDots[i].active = false; |
| 137 | } |
| 138 | |
| 139 | int GUIPatternPassword::SetRenderPos(int x, int y, int w, int h) |
| 140 | { |
| 141 | mRenderX = x; |
| 142 | mRenderY = y; |
| 143 | |
| 144 | if (w || h) |
| 145 | { |
| 146 | mRenderW = w; |
| 147 | mRenderH = h; |
| 148 | |
| 149 | mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 150 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 151 | } |
| 152 | |
| 153 | CalculateDotPositions(); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | void GUIPatternPassword::CalculateDotPositions(void) |
| 158 | { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 159 | const int num_gaps = mGridSize - 1; |
| 160 | const int step_x = (mRenderW - mDotRadius*2) / num_gaps; |
| 161 | const int step_y = (mRenderH - mDotRadius*2) / num_gaps; |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 162 | int x = mRenderX; |
| 163 | int y = mRenderY; |
| 164 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 165 | /* Order is important for keyphrase generation: |
| 166 | * |
| 167 | * 0 1 2 3 ... n-1 |
| 168 | * n n+1 n+2 n+3 ... 2n-1 |
| 169 | * 2n 2n+1 2n+2 2n+3 ... 3n-1 |
| 170 | * 3n 3n+1 3n+2 3n+3 ... 4n-1 |
| 171 | * : : : : |
| 172 | * n*n-1 |
| 173 | */ |
| 174 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 175 | for (size_t r = 0; r < mGridSize; ++r) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 176 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 177 | for (size_t c = 0; c < mGridSize; ++c) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 178 | { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 179 | mDots[mGridSize*r + c].x = x; |
| 180 | mDots[mGridSize*r + c].y = y; |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 181 | x += step_x; |
| 182 | } |
| 183 | x = mRenderX; |
| 184 | y += step_y; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | int GUIPatternPassword::Render(void) |
| 189 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 190 | if (!isConditionTrue()) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 191 | return 0; |
| 192 | |
| 193 | gr_color(mLineColor.red, mLineColor.green, mLineColor.blue, mLineColor.alpha); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 194 | for (size_t i = 1; i < mConnectedDotsLen; ++i) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 195 | const Dot& dp = mDots[mConnectedDots[i-1]]; |
| 196 | const Dot& dc = mDots[mConnectedDots[i]]; |
| 197 | gr_line(dp.x + mDotRadius, dp.y + mDotRadius, dc.x + mDotRadius, dc.y + mDotRadius, mLineWidth); |
| 198 | } |
| 199 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 200 | if (mConnectedDotsLen > 0 && mTrackingTouch) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 201 | const Dot& dc = mDots[mConnectedDots[mConnectedDotsLen-1]]; |
| 202 | gr_line(dc.x + mDotRadius, dc.y + mDotRadius, mCurLineX, mCurLineY, mLineWidth); |
| 203 | } |
| 204 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 205 | for (size_t i = 0; i < mGridSize * mGridSize; ++i) { |
| 206 | if (mDotCircle) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 207 | gr_blit(mDotCircle, 0, 0, gr_get_width(mDotCircle), gr_get_height(mDotCircle), mDots[i].x, mDots[i].y); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 208 | if (mDots[i].active) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 209 | gr_blit(mActiveDotCircle, 0, 0, gr_get_width(mActiveDotCircle), gr_get_height(mActiveDotCircle), mDots[i].x + mDotRadius/2, mDots[i].y + mDotRadius/2); |
| 210 | } |
| 211 | } else { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 212 | if (mDots[i].active && mActiveDotImage && mActiveDotImage->GetResource()) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 213 | gr_blit(mActiveDotImage->GetResource(), 0, 0, mActiveDotImage->GetWidth(), mActiveDotImage->GetHeight(), |
| 214 | mDots[i].x + (mDotRadius - mActiveDotImage->GetWidth()/2), mDots[i].y + (mDotRadius - mActiveDotImage->GetHeight()/2)); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 215 | } else if (mDotImage && mDotImage->GetResource()) { |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 216 | gr_blit(mDotImage->GetResource(), 0, 0, mDotImage->GetWidth(), mDotImage->GetHeight(), mDots[i].x, mDots[i].y); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | int GUIPatternPassword::Update(void) |
| 224 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 225 | if (!isConditionTrue()) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 226 | return 0; |
| 227 | |
| 228 | int res = mNeedRender ? 2 : 1; |
| 229 | mNeedRender = false; |
| 230 | return res; |
| 231 | } |
| 232 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 233 | void GUIPatternPassword::Resize(size_t n) { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 234 | if (mGridSize == n) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 235 | return; |
| 236 | |
| 237 | delete[] mDots; |
| 238 | delete[] mConnectedDots; |
| 239 | |
| 240 | mGridSize = n; |
| 241 | mDots = new Dot[n*n]; |
| 242 | mConnectedDots = new int[n*n]; |
| 243 | |
| 244 | ResetActiveDots(); |
| 245 | CalculateDotPositions(); |
| 246 | mTrackingTouch = false; |
| 247 | mNeedRender = true; |
| 248 | } |
| 249 | |
| 250 | static int pow(int x, int i) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 251 | { |
nailyk | 61c7c5c | 2016-08-19 13:36:07 +0200 | [diff] [blame] | 252 | int result = 1; |
| 253 | if (i<0) |
| 254 | return 0; |
| 255 | while(i-- > 0) |
| 256 | result *= x; |
| 257 | return result; |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static bool IsInCircle(int x, int y, int ox, int oy, int r) |
| 261 | { |
| 262 | return pow(x - ox, 2) + pow(y - oy, 2) <= pow(r, 2); |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | int GUIPatternPassword::InDot(int x, int y) |
| 266 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 267 | for (size_t i = 0; i < mGridSize * mGridSize; ++i) { |
| 268 | if (IsInCircle(x, y, mDots[i].x + mDotRadius, mDots[i].y + mDotRadius, mDotRadius*3)) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 269 | return i; |
| 270 | } |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | bool GUIPatternPassword::DotUsed(int dot_idx) |
| 275 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 276 | for (size_t i = 0; i < mConnectedDotsLen; ++i) { |
| 277 | if (mConnectedDots[i] == dot_idx) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | void GUIPatternPassword::ConnectDot(int dot_idx) |
| 284 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 285 | if (mConnectedDotsLen >= mGridSize * mGridSize) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 286 | { |
| 287 | LOGERR("mConnectedDots in GUIPatternPassword has overflown!\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | mConnectedDots[mConnectedDotsLen++] = dot_idx; |
| 292 | mDots[dot_idx].active = true; |
| 293 | } |
| 294 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 295 | void GUIPatternPassword::ConnectIntermediateDots(int next_dot_idx) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 296 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 297 | if (mConnectedDotsLen == 0) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 298 | return; |
| 299 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 300 | const int prev_dot_idx = mConnectedDots[mConnectedDotsLen-1]; |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 301 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 302 | int px = prev_dot_idx % mGridSize; |
| 303 | int py = prev_dot_idx / mGridSize; |
| 304 | |
| 305 | int nx = next_dot_idx % mGridSize; |
| 306 | int ny = next_dot_idx / mGridSize; |
| 307 | |
| 308 | /* |
| 309 | * We connect all dots that are in a straight line between the previous dot |
| 310 | * and the next one. This is simple for 3x3, but is more complicated for |
| 311 | * larger grids. |
| 312 | * |
| 313 | * Weirdly, Android doesn't do the logical thing when it comes to connecting |
| 314 | * dots between two points. Rather than simply adding all points that lie |
| 315 | * on the line between the start and end points, it instead only connects |
| 316 | * dots that are adjacent in only three directions -- horizontal, vertical |
| 317 | * and diagonal (45°). |
| 318 | * |
| 319 | * So we can just iterate over the correct axes, taking care to ensure that |
| 320 | * the order in which the intermediate points are added to the pattern is |
| 321 | * correct. |
| 322 | */ |
| 323 | |
| 324 | int x = px; |
| 325 | int y = py; |
| 326 | |
| 327 | int Dx = (nx > px) ? 1 : -1; |
| 328 | int Dy = (ny > py) ? 1 : -1; |
| 329 | |
| 330 | // Vertical lines. |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 331 | if (px == nx) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 332 | Dx = 0; |
| 333 | |
| 334 | // Horizontal lines. |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 335 | else if (py == ny) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 336 | Dy = 0; |
| 337 | |
| 338 | // Diagonal lines (|∆x| = |∆y|). |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 339 | else if (abs(px - nx) == abs(py - ny)) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 340 | ; |
| 341 | |
| 342 | // No valid intermediate dots. |
| 343 | else |
Vojtech Bocek | e8c3927 | 2015-03-15 15:25:37 +0100 | [diff] [blame] | 344 | return; |
Vojtech Bocek | e8c3927 | 2015-03-15 15:25:37 +0100 | [diff] [blame] | 345 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 346 | // Iterate along axis, adding dots in the correct order. |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 347 | while ((Dy == 0 || y != ny - Dy) && (Dx == 0 || x != nx - Dx)) { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 348 | x += Dx; |
| 349 | y += Dy; |
| 350 | |
| 351 | int idx = mGridSize * y + x; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 352 | if (!DotUsed(idx)) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 353 | ConnectDot(idx); |
| 354 | } |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int GUIPatternPassword::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 358 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 359 | if (!isConditionTrue()) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 360 | return -1; |
| 361 | |
| 362 | switch (state) |
| 363 | { |
| 364 | case TOUCH_START: |
| 365 | { |
| 366 | const int dot_idx = InDot(x, y); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 367 | if (dot_idx == -1) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | mTrackingTouch = true; |
| 371 | ResetActiveDots(); |
| 372 | ConnectDot(dot_idx); |
| 373 | DataManager::Vibrate("tw_button_vibrate"); |
| 374 | mCurLineX = x; |
| 375 | mCurLineY = y; |
| 376 | mNeedRender = true; |
| 377 | break; |
| 378 | } |
| 379 | case TOUCH_DRAG: |
| 380 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 381 | if (!mTrackingTouch) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | const int dot_idx = InDot(x, y); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 385 | if (dot_idx != -1 && !DotUsed(dot_idx)) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 386 | { |
| 387 | ConnectIntermediateDots(dot_idx); |
| 388 | ConnectDot(dot_idx); |
| 389 | DataManager::Vibrate("tw_button_vibrate"); |
| 390 | } |
| 391 | |
| 392 | mCurLineX = x; |
| 393 | mCurLineY = y; |
| 394 | mNeedRender = true; |
| 395 | break; |
| 396 | } |
| 397 | case TOUCH_RELEASE: |
| 398 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 399 | if (!mTrackingTouch) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | mNeedRender = true; |
| 403 | mTrackingTouch = false; |
| 404 | PatternDrawn(); |
| 405 | ResetActiveDots(); |
| 406 | break; |
| 407 | } |
| 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 414 | int GUIPatternPassword::NotifyVarChange(const std::string& varName, const std::string& value) |
| 415 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 416 | if (!isConditionTrue()) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 419 | if (varName == mSizeVar) { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 420 | Resize(atoi(value.c_str())); |
| 421 | mUpdate = true; |
| 422 | } |
| 423 | return 0; |
| 424 | } |
| 425 | |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 426 | static unsigned int getSDKVersion(void) { |
| 427 | unsigned int sdkver = 23; |
| 428 | string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk"); |
| 429 | if (!sdkverstr.empty()) { |
| 430 | sdkver = (unsigned int)strtoull(sdkverstr.c_str(), NULL, 10); |
| 431 | sdkver = (sdkver != 0) ? sdkver : 23; |
| 432 | } |
| 433 | LOGINFO("sdk version is %u\n", sdkver); |
| 434 | return sdkver; |
| 435 | } |
| 436 | |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 437 | std::string GUIPatternPassword::GeneratePassphrase() |
| 438 | { |
| 439 | char pattern[mConnectedDotsLen]; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 440 | for (size_t i = 0; i < mConnectedDotsLen; i++) { |
| 441 | pattern[i] = (char) mConnectedDots[i]; |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | std::stringstream pass; |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 445 | char buffer[3] = {0}; |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 446 | |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 447 | if ((mGridSize == 3) || (getSDKVersion() >= 23)) { |
| 448 | // Marshmallow uses a consistent method |
| 449 | for (size_t i = 0; i < mConnectedDotsLen; i++) { |
| 450 | buffer[0] = (pattern[i] & 0xff) + '1'; |
| 451 | pass << std::string(buffer); |
| 452 | } |
| 453 | } else { |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 454 | /* |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 455 | * Okay, rant time for pre-Marshmallow ROMs. |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 456 | * It turns out that Android and CyanogenMod have *two* separate methods |
| 457 | * for generating passphrases from patterns. This is a legacy issue, as |
| 458 | * Android only supports 3x3 grids, and so we need to support both. |
| 459 | * Luckily, CyanogenMod is in the same boat as us and needs to support |
| 460 | * Android's 3x3 encryption style. |
| 461 | * |
| 462 | * In order to generate a 3x3 passphrase, add 1 to each dot index |
| 463 | * and concatenate the string representation of the integers. No |
| 464 | * padding should be added. |
| 465 | * |
| 466 | * For *all* other NxN passphrases (until a 16x16 grid comes along), |
| 467 | * they are generated by taking "%.2x" for each dot index and |
| 468 | * concatenating the results (without adding 1). |
| 469 | */ |
Sultan Qasim Khan | 7a48a66 | 2016-02-12 19:17:38 -0500 | [diff] [blame] | 470 | for (size_t i = 0; i < mConnectedDotsLen; i++) { |
| 471 | snprintf(buffer, 3, "%.2x", pattern[i] & 0xff); |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 472 | pass << std::string(buffer); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return pass.str(); |
| 477 | } |
| 478 | |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 479 | void GUIPatternPassword::PatternDrawn() |
| 480 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 481 | if (!mPassVar.empty() && mConnectedDotsLen > 0) |
Aleksa Sarai | b25a183 | 2015-12-31 17:36:00 +0100 | [diff] [blame] | 482 | DataManager::SetValue(mPassVar, GeneratePassphrase()); |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 483 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 484 | if (mAction) |
Vojtech Bocek | 7e11ac5 | 2015-03-05 23:21:49 +0100 | [diff] [blame] | 485 | mAction->doActions(); |
| 486 | } |